पाठ 3 / 26
Recognising Patterns
Wording and constraints are clues.
Map clues to techniques
Most interview problems reuse a small set of patterns. Clues in the wording point to them: "sorted array" suggests binary search or two pointers, "contiguous subarray" suggests sliding window or prefix sums, "top k" suggests a heap, "all combinations" suggests backtracking, "minimum steps in a grid" suggests BFS, "prerequisites" suggests topological sort, and "number of ways / minimum cost" suggests dynamic programming.
Clue to pattern cheat sheet
A starting point, not a rule.
pair / complement / seen before -> hash map
subarray sum equals k -> prefix sums + hash map
sorted input, pair or triplet -> two pointers
longest/shortest contiguous window -> sliding window
sorted / monotonic answer space -> binary search
next greater / smaller element -> monotonic stack
cycle or middle of linked list -> fast & slow pointers
shortest path, unweighted -> BFS
connected components -> DFS / union-find
dependencies / ordering -> topological sort
weighted shortest path -> Dijkstra
top k / k-th largest / merge k lists -> heap
overlapping ranges -> sort + intervals
all subsets / permutations -> backtracking
count ways / min cost / max value -> dynamic programmingPractise by pattern, then mix
Solve several problems per pattern to learn it, then practise mixed sets so you learn to recognise patterns cold.
त्वरित जाँच: Which pattern does "longest substring with at most k distinct characters" suggest?
- Backtracking
- Topological sort
- Dijkstra
- Sliding window
Answer
Sliding window — Contiguous plus longest.