AI Hints
Home›DSA in Python Tutorial›Puzzles, Bits and Strategy
DSA in Python · Lesson 102 of 132

Choosing a Technique

Which tool for which problem.

⏱ 2 min read▶ Video at 2:02:00🎓 Free
Watch this lesson in the DSA in Python courseWatch on YouTube ↗Full DSA in Python course

You've learned a whole toolbox. The hardest part of a new problem is knowing which tool to reach for. Luckily, problems leave clues in how they're worded.

Choosing a Technique
From the video: Choosing a Technique
  • A whole toolbox
  • Which tool?
  • Problems leave clues

Clues and tools

Here's a cheat sheet. Sorted input suggests binary search, or two pointers. A continuous stretch suggests a sliding window. Have I seen this before, suggests a hash set or dictionary. Top K suggests a heap. Shortest steps suggests breadth first search. Every possible answer suggests backtracking. And count the ways, or the best total, suggests dynamic programming.

The problem saysTry
sorted, find a pairtwo pointers, binary search
substring, subarray, in a rowsliding window
seen before, duplicates, pairshash set or dict
top k, smallest, most urgentheap
shortest steps, levelsBFS
all combinationsbacktracking
number of ways, best totaldynamic programming

When you are stuck

If no tool jumps out, start with a slow, simple solution, even O of N squared. Then ask, where is the repeated work? Can a dictionary remember it? Would sorting help? Can I reuse smaller answers? Improving a working solution is far easier than inventing a perfect one.

keyvalue
Start simplebrute force first
Find repeatswhat's recalculated?
Rememberdict, cache
Reusesmaller answers

Summary

Read the clues in the problem, and match them to a tool. If stuck, start simple and improve. Next, a new section. Interview practice.

Key takeaways
  • Read the clues
  • Match a tool
  • Brute force, then improve
Next up: How to Solve Any Problem — a step by step method