SproutStackSproutStack home

How to Pick a Pattern

5 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

Read the problem like a detective

Every coding problem leaks hints about its solution — specific words that veterans translate instantly into patterns. Learning this translation table is the highest-leverage DSA skill there is: it turns "I've never seen this" into "this is a sliding window problem wearing a costume."

The translation table (memorize this)

  • "Sorted array" + pair/palindrome → Two Pointers (opposite ends converge).
  • "Contiguous subarray/substring," "longest/shortest with constraint" → Sliding Window (expand right, shrink left).
  • "Counts," "duplicates," "pairs summing to," "first unique" → Hash Map (remember what you've seen).
  • "Range sum queries," "sum over intervals" → Prefix Sum (precompute, subtract).
  • "Search in sorted/rotated," "minimize the maximum" → Binary Search (halve the suspects).
  • "Top K," "median of stream" → Heap (boundary maintenance).
  • "All combinations/permutations," "try every choice" → Backtracking (decide, recurse, undo).
  • "Overlapping subproblems," "ways to reach," "min/max cost path" → DP (remember subanswers).

Three problems, decoded

  • "Find the length of the longest substring with at most two distinct characters." "Longest" plus "substring" means a contiguous range under a constraint: sliding window. Grow the right edge, and shrink the left whenever a third distinct character appears.
  • "Return true if any two numbers in the array add up to k." "Two numbers that sum to" with no mention of sorting: hash map. For each number, check whether k - number has been seen.
  • "Given a sorted array, return the squares of each number, also sorted." "Sorted" plus the fact that the largest squares sit at the two ends (big negatives and big positives): two pointers, moving inward from both ends.

In each case the decision came from two or three words in the prompt, before any code.

When the table fails you

No keyword matches? Fall back to first principles: write the brute force, then ask "what work am I repeating?" Recomputing window sums → sliding window. Rechecking pairs → hash map. Re-solving subproblems → memoization. The pattern is whatever eliminates the repetition — the table just names it faster.

If stuck: brute first, ask "am I rechecking? Can a map/window remember it?"

Constraints are hints too

The size limits in a problem statement quietly tell you what complexity is expected. A rough rule: a typical judge runs on the order of a hundred million simple operations per second.

  • n up to about 20: exponential approaches are fine, so backtracking or trying every subset is on the table.
  • n up to a few thousand: O(n²) usually passes.
  • n up to 100,000 or a million: you need O(n log n) or O(n). This is where sorting, hash maps, two pointers, and windows come in.
  • Values limited to a small range (say, 0 to 100): a simple counting array can replace sorting or hashing.

Reading the constraints first often rules out half the patterns before you start.

How to practice recognition

Pattern spotting improves fastest when practiced on its own. Take 20 problem statements, read each one, and write down the pattern you would try, without solving anything. Then check against solutions. Ten minutes a day of this builds the instinct far faster than grinding full solutions alone.

Remember this

  • Keywords map to patterns; repetition analysis finds patterns from scratch.
  • State your guess early ("this smells like two pointers because…") — interviewers redirect wrong guesses kindly.

Check your understanding

5 questions · first-try answers earn the most XP

1. A prompt says "longest substring without repeating characters". Which pattern does that hint at?
2. What do "counts", "duplicates" and "first unique" all signal?
3. Which signals in a problem statement point toward needing backtracking?
4. When no keyword matches, what does the chapter recommend?
5. Why is stating a pattern guess early useful, even when it turns out wrong?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

End of Start Here

Take the section checkpoint

A short mixed quiz on all 2 chapters, to check you are ready to move on.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.