Interviewing
Interview patterns
Learn the trigger, not a code dump. Two pointers, windows, hashing, graphs and a few more cover most mediums.
Interview problems cluster. Pattern fluency means you hear "longest substring without repeating characters" and reach for a sliding window with a last-seen index map. Memorizing solutions fails when the problem is twisted 15 degrees.
Concept
A pattern trigger is the shape of the input and the question that points at a template. Keep a cheat sheet of triggers, not a repo of solutions you cannot regenerate. The problem-solving process is how you apply the template.
The big idea
You do not need every hard DP variant. You need fluent mediums in the first eight rows below and a working acquaintance with the rest.
| Pattern | Trigger | Typical complexity |
|---|---|---|
| Two pointers | Sorted array, pairs, palindromes, partition | Often O(n) |
| Sliding window | Substring/subarray with a constraint | O(n) with a map/set |
| Hashing | Frequency, complement lookup, anagrams | Expected O(n) |
| Binary search | Sorted search space, "minimize max" | O(log n) on the space |
| BFS / DFS | Graphs, grids, trees, unweighted shortest path | O(V+E) |
| Backtracking | Combinations, permutations, constraints | Exponential; prune hard |
| Heap | Top-K, merge K lists, running median | Often O(n log k) |
| Intervals | Merge, insert, meeting rooms | Sort then sweep O(n log n) |
| Monotonic stack | Next greater, histogram rectangle | O(n) |
| Union-find | Connectivity, dynamic merges | Nearly O(n) with compression |
| Topological sort | Dependencies, build order | O(V+E) |
| Dynamic programming | Optimal substructure + overlapping subproblems | Often O(n)–O(n²) |
Drill recognition with spaced repetition: revisit tough problems after 1 day, 3 days and 1 week.
Walking through a window
Many substring problems share one template: expand the right edge, shrink the left until the window is valid, track a best answer.
Example
Longest substring without repeating characters: "abcabcbb" → 3 ("abc").
Keep left, a map of char → last index, and best. For each right, if s[right] was seen at index ≥ left, move left to that index + 1. Update last index and best = max(best, right - left + 1). Each index moves at most once → O(n) time, O(min(n, alphabet)) space.
def length_of_longest_substring(s: str) -> int:
last: dict[str, int] = {}
left = 0
best = 0
for right, ch in enumerate(s):
if ch in last and last[ch] >= left:
left = last[ch] + 1
last[ch] = right
best = max(best, right - left + 1)
return bestOutput for "abcabcbb":
3Say the invariant out loud: "window [left, right] always has unique characters." Invariants are how interviewers see that you are not guessing.
For grids and graphs, default to BFS for shortest path in unweighted graphs and DFS/backtracking for all paths or components. Clarify: directed vs undirected, weighted vs not, can you revisit nodes? A BFS skeleton (queue + visited) should be writable in under three minutes. For DP, force the questions: what is the state, the transition, the base case? Can you reduce space? If you cannot answer state and transition in two sentences, stay on the recursive definition longer.
Common mistakes
- Code dumps instead of triggers. You cannot regenerate the solution when the prompt twists.
- Skipping the invariant. The window "works" until an off-by-one you never stated.
- Coding DP before the state is a sentence. You will debug a fog.
Check yourself
- You hear "longest substring with at most K distinct characters." Which pattern?
Sliding window, with a map of counts, shrinking left when the distinct count exceeds K.
- Why is a sliding window often
O(n)?
Each index enters and leaves the window at most once.
Key takeaways
- Learn triggers, not memorized listings.
- Fluent mediums in windows, two pointers, hashing, graphs, heaps and intervals beat shallow hards.
- State the invariant (or DP state and transition) before you type.
Further learning
NeetCode roadmapPractice coding interviews with structured courses, AI-assisted assessments, head-to-head Versus mode, and 1,000+ problems.NeetCodeNext: communicating while you code.
Related
The interview problem-solving process
Clarify, work examples, state a brute force, then optimize, code, test and analyze. Interviewers grade the path.
Coding interviews
Patterns, problem-solving process and communication for algorithm interviews — plus how practical coding rounds differ.
Data structures in practice
Choosing data structures by memory layout, access pattern and the operations that matter, with the common ones for arrays, hash maps, heaps, trees, caches and graphs.