LeetCode / NeetCode 150 — Interview Grill (Pattern Recognition)
150+ pattern-recognition questions — read the problem statement, name the pattern in <30 seconds. Pair with
LEETCODE_PATTERNS_DEEP_DIVE.md.
The point of these questions is not solving — it’s identifying which template to deploy. Speed of recognition is the leverage skill.
Section A — The triage (Q1–10)
- List the 7 questions in the 30-second triage in order.
- For each input shape, name two candidate patterns: single array, sorted array, 2D grid, tree, graph, string, multiple intervals.
- For each output shape, name two patterns: count, min/max, all combinations, single index/pair.
- — what algorithmic complexity is acceptable?
- — what’s your target complexity?
- “Subarray” / “contiguous” — which pattern?
- “Subsequence” — which pattern?
- “Top-K” — which pattern?
- “Connected” — which pattern(s)?
- “Shortest path, weighted, no negatives” — which?
Saying it out loud. (What the triage sounds like at the whiteboard.) “Okay — input’s a single array, output’s a count, n goes up to ten to the fifth. That constraint’s the loudest signal in the problem: it kills anything quadratic, so I’m aiming for linear or n-log-n. And the word ‘contiguous’ means subarray, not subsequence, which points me at sliding window rather than DP.” Thirty seconds, said out loud, and now the interviewer can correct me before I’ve written a line. The number to anchor on: n at ten to the fifth means n log n or better, n at ten thousand means n squared is probably fine, and n at twenty or under is an explicit invitation to go exponential.
Section B — Pattern recognition: arrays / hashing / two pointers / sliding window (Q11–25)
- “Find pair summing to target in sorted array” → pattern?
- “Find pair summing to target in unsorted array” → pattern?
- “Group anagrams” → pattern + key idea?
- “Top K frequent elements” → two patterns + complexities?
- “Longest substring without repeating chars” → pattern?
- “Min window substring containing all chars in T” → pattern?
- “Longest repeating character replacement (k changes)” → pattern + invariant?
- “Container with most water” → pattern + which side moves?
- “3Sum” → pattern + complexity?
- “Trapping rain water” → two-pass approach + alternative?
- “Best time to buy/sell stock (one transaction)” → pattern + state?
- “Maximum subarray sum” → name the algorithm.
- “Product of array except self” → pattern + technique?
- “Longest consecutive sequence” → pattern + key trick?
- “Permutation in string” → pattern?
Saying it out loud. (Narrating the sorted-versus-unsorted fork.) “The brute force is every pair, n squared. If the array’s sorted I don’t need extra memory at all — two pointers from the ends, and the sortedness tells me which one to move, so it’s O of n time and O of 1 space. If it’s unsorted, I trade space for time instead: one pass with a hash map from value to index, checking for the complement as I go. Still O of n, but now O of n space.” That’s the fork, and saying which one you’re in and why is the actual scored moment. The trap to name out loud is inserting into the map before you check, which lets an element pair with itself.
Section C — Stack / monotonic stack (Q26–33)
- “Valid parentheses” → pattern?
- “Min stack” — two ways to design?
- “Daily temperatures” → pattern + monotonic-which-way?
- “Next greater element” → pattern?
- “Largest rectangle in histogram” → pattern + key insight?
- “Car fleet” → preprocessing + pattern?
- “Evaluate Reverse Polish Notation” → pattern?
- “Generate parentheses” → pattern (this is a trap)?
Saying it out loud. (Justifying the amortized bound before they ask.) “I know this looks quadratic because there’s a while loop inside the for loop, but each element gets pushed exactly once and popped at most once — so it’s O of n total, amortized.” Say that unprompted; it’s the single line that separates someone who memorized the monotonic stack template from someone who understands it. Then name the invariant: the stack stays decreasing, so anything I pop has just found its next greater element. And handle the leftovers explicitly — whatever’s still on the stack at the end never found a match and gets the default, usually negative one.
Section D — Binary search (Q34–43)
- When does binary search apply? Two requirements.
- “Find min in rotated sorted array” → pattern + key comparison?
- “Search in rotated sorted array” → pattern?
- “Search 2D matrix” → key trick?
- “Koko eating bananas” → pattern + what to BS on?
- “Median of two sorted arrays” → pattern + complexity?
- “Time-based key-value store” → pattern + per-key structure?
- “Capacity to ship packages within D days” → pattern?
- “Find peak element” → pattern + invariant?
- Difference between
l <= randl < rloop forms — when each?
Saying it out loud. (Keeping yourself out of the infinite loop.) “I’m going to write the boundary-finding version rather than exact-match, because it generalizes to ‘first element at least x’ without extra branches. Low inclusive, high exclusive, mid is low plus high-minus-low over two so I don’t overflow in languages where that matters. My invariant is that the answer always lives in the range low to high — that’s what guarantees the range shrinks every iteration.” Then the harder variant to be ready for: binary search on the answer, where the array isn’t the search space at all and you’re bisecting a numeric range with a monotonic feasibility check. O of log n either way, O of 1 space.
Section E — Linked list (Q44–53)
- “Reverse linked list” → recursive + iterative templates?
- “Detect cycle” → algorithm name?
- “Find cycle entry” → algorithm + key step?
- “Merge two sorted lists” → pattern + dummy node?
- “Reorder list” → 3-step decomposition?
- “Remove Nth from end” → pattern?
- “Copy list with random pointer” → two approaches?
- “LRU cache” → data structures + complexities?
- “Merge K sorted lists” → two approaches + complexities?
- “Find the duplicate number (Floyd cycle on implicit graph)” → pattern + insight?
Saying it out loud. (Two habits, said before you type.) “I’ll add a dummy head node first — it makes inserting or deleting at the front behave identically to the middle, which kills a whole family of null checks.” And for anything about the middle, cycles, or the kth-from-end: two pointers, either at different speeds or at a fixed offset. Floyd’s cycle detection is the one to be able to justify — the fast pointer gains one position per step on the slow one, so inside a cycle it must eventually land on it. O of n time, O of 1 space, and that constant space is the whole reason to prefer it over a visited set. Edge cases: empty list, single node, and modifying the head itself.
Section F — Trees (Q54–67)
- DFS template (recursive postorder, with return).
- BFS level-order template.
- “Invert binary tree” → recursion?
- “Maximum depth” — recurrence?
- “Diameter of binary tree” — what to track during recursion?
- “Balanced binary tree” — sentinel pattern?
- “Same tree” / “subtree” — pattern?
- “LCA in BST” — pattern + key BST property?
- “LCA in general tree” — DFS pattern + return condition?
- “Validate BST” — pattern + key parameter?
- “Right side view” — DFS or BFS approach?
- “Construct from preorder + inorder” — pattern?
- “Kth smallest in BST” — pattern?
- “Serialize / deserialize” — encoding choice?
Saying it out loud. (The question that picks your traversal.) “Does this node need information from its children, or from its parent? Children-up is post-order — I recurse, get values back, and combine. Parent-down is passing state as an argument, like the min-max bounds for validating a BST. And anything about levels or minimum depth is BFS with a queue, not recursion at all.” That one question decides the code shape and it’s worth asking out loud. Complexity is O of n since every node gets visited once; space is O of h for the stack. Name the worst case: a degenerate, list-shaped tree makes h equal n, which will blow Python’s default thousand-frame recursion limit.
Section G — Tries (Q68–72)
- When to reach for a trie?
- Insert / search / starts_with — complexities?
- “Add and search word with
.wildcard” → pattern + DFS technique? - “Word search II” → pattern + what’s the trie used for?
- Trade-offs between trie and hash set.
Saying it out loud. (Justifying the data structure choice.) “A hash set can answer ‘does this word exist’ in O of L, but it can’t answer ‘does anything start with this prefix’ without scanning everything. That’s what the trie buys — insert and search are O of L in word length and completely independent of how many words are stored.” Then name the price you’re paying, because interviewers want the tradeoff: memory, roughly a node per character in the worst case, which is much heavier than a set. So you build one only when prefix queries are genuinely in the requirements. Edge cases: the empty string, and a stored word that’s a proper prefix of another still needing its end-of-word flag.
Section H — Heap / priority queue (Q73–82)
- “Kth largest element in array” → two approaches + complexities?
- “Last stone weight” → pattern?
- “K closest points to origin” → pattern + heap size?
- “Task scheduler” → pattern + key data structures?
- “Find median from data stream” → pattern + invariant?
- “Merge K sorted lists” → heap formulation?
- “Top K frequent elements” → two approaches?
- “Design Twitter” → pattern?
- Python
heapq— min or max heap by default? heapifycomplexity?
Saying it out loud. (Why not just sort.) “I could sort and take the last k, that’s n log n. But I only need the top k, so I’ll hold a min-heap capped at size k — push everything, pop whenever it exceeds k — and the weakest of my current best k is always right on top where comparing is cheap. That’s O of n log k, which is a real win when k is small, and O of k space.” Then the language detail worth saying so the interviewer knows you’ve actually written it: Python’s heapq is min-only, so a max-heap means pushing negated values. Edge cases: k larger than the array, and ties at the boundary.
Section I — Backtracking (Q83–93)
- When to reach for backtracking?
- “Subsets” — include/exclude template?
- “Permutations” — swap-in-place vs used-array?
- “Combination sum” — start index + when to recurse?
- “Combination sum II (with duplicates)” — sort + skip-when?
- “Word search” — DFS + visited marker pattern?
- “Palindrome partitioning” — what do you try at each step?
- “N-queens” — what state to track for check?
- “Letter combinations of phone number” — recursion vs iterative?
- “Sudoku solver” — what 3 sets per cell?
- Common bug in backtracking when storing solutions?
Saying it out loud. (Giving yourself permission to be exponential.) “n is twenty or under and the output is ‘all of them,’ so exponential is the intended answer and I shouldn’t waste time hunting for something polynomial.” Then narrate the shape: choose, recurse, un-choose, with a start index so I never revisit an earlier element and generate the same set in a different order. State the complexity as roughly n times two-to-the-n for subsets or n factorial for permutations. Two bugs to call out before they happen: append a copy of the path at the leaf, because you’re about to mutate it; and for duplicate inputs, sort first and skip an element equal to its predecessor at the same level.
Section J — Graphs (Q94–106)
- DFS template. BFS template.
- “Number of islands” → pattern?
- “Clone graph” → pattern + key data structure?
- “Pacific Atlantic water flow” → pattern (multi-source BFS)?
- “Surrounded regions” → key trick?
- “Rotting oranges” → pattern + state to track?
- “Walls and gates” → pattern?
- “Course schedule” → pattern + algorithm?
- “Course schedule II” → return what?
- “Word ladder” → pattern + how to build the graph?
- “Number of connected components” — two methods?
- “Graph valid tree” — three conditions?
- “Redundant connection” → pattern?
Saying it out loud. (The one line that fixes the complexity.) “Connectivity means graph, and a grid is just a graph whose neighbors are the four adjacent cells. BFS if I need shortest path in an unweighted graph, DFS if I only need to explore or count components — both O of V plus E. The thing I’m being careful about: I mark a node visited when I enqueue it, not when I dequeue it.” That distinction is worth saying explicitly, because marking on dequeue lets the same node get queued many times over and quietly wrecks the bound. Edge cases: disconnected components, so loop over every possible start; self-loops; and an empty grid.
Section K — Advanced graphs (Q107–115)
- Topological sort — Kahn’s vs DFS-based?
- Dijkstra — when applicable + complexity?
- Bellman-Ford — when over Dijkstra + complexity?
- Floyd-Warshall — when + complexity?
- MST — Kruskal vs Prim?
- “Network delay time” → pattern?
- “Cheapest flights within K stops” → pattern?
- “Min cost to connect all points” → pattern?
- “Reconstruct itinerary” → algorithm name?
Saying it out loud. (Naming which constraint you’re reacting to.) “Dependencies and an ordering means topological sort — Kahn’s with in-degrees, O of V plus E, and if the queue drains before I’ve emitted every node there’s a cycle, which is usually what the problem was really asking. Weighted with non-negative edges is Dijkstra, a heap-driven BFS at E log V. Negative edges break Dijkstra’s greedy assumption outright, so that’s Bellman-Ford at V times E, which also detects negative cycles with one extra relaxation pass.” The mistake worth pre-empting out loud is reaching for Dijkstra on an unweighted graph — plain BFS gets the identical answer without the log factor.
Section L — 1D DP (Q116–127)
- When to reach for 1D DP?
- State definition:
dp[i]for “Climbing Stairs”. - Coin Change — recurrence?
- Coin Change II (number of ways) — what changes?
- House Robber — recurrence?
- House Robber II — how to handle the circular constraint?
- Decode Ways — how to handle “0”?
- Word Break — recurrence + dictionary lookup?
- LIS — recurrence + trick name?
- Maximum Product Subarray — what state to track?
- Partition Equal Subset Sum — reduces to which classical?
- Longest palindromic substring — expand-around-centers vs DP?
Saying it out loud. (Defining the state before writing anything.) “Let me define dp of i as the answer for the first i elements — I want to say that before I write code, because a fuzzy state definition is where DP goes wrong. The recurrence: at i I either take this element and add dp of i minus two, or skip it and keep dp of i minus one, so dp of i is the max. Base cases are dp of zero and dp of one, and that’s where the bug usually lives.” O of n time and O of n space, then offer the improvement: it only ever looks back two positions, so it collapses to two variables and O of 1 space. And name the discovery path — brute-force recursion, spot the overlapping subproblems, memoize, flip to bottom-up.
Section M — 2D DP (Q128–138)
- When to reach for 2D DP?
- LCS — recurrence on equal vs unequal characters?
- Edit Distance — three operation costs.
- Distinct Subsequences — what’s
dp[i][j]? - Interleaving String — boolean DP, what’s the recurrence?
- Buy/sell with cooldown — state dimension?
- Best Time IV (k transactions) — state?
- Target Sum — reduces to subset sum count?
- Burst Balloons — why range DP, not interval-greedy?
- Regular Expression Matching — recurrence on
*? - Longest Increasing Path in matrix — DFS + memo or pure DP?
Saying it out loud. (Two sequences means two indices.) “dp of i, j is the answer for the first i characters of one string against the first j of the other. If the characters match I extend the diagonal — dp of i minus one, j minus one, plus one. If they don’t, I take the better of dropping a character from either side. The zero row and column encode ‘one string is empty,’ which is exactly my base case.” O of m times n in both time and space, and then offer the follow-up before they ask: each row only depends on the row above, so it rolls down to O of n space. The edge case that’s already handled if you set the base row correctly is empty input.
Section N — Greedy, intervals, math, bits (Q139–155)
- When does greedy work? How do you prove it?
- Maximum subarray (Kadane) — running sum logic?
- Jump Game II — greedy invariant?
- Gas station — pattern?
- Hand of Straights — pattern + data structure?
- Partition Labels — preprocessing + sweep?
- Insert Interval — three phases?
- Merge Intervals — sort by what?
- Non-overlapping intervals — greedy + sort by what?
- Meeting rooms II — sweep-line + heap method?
- Min interval to include each query — pattern?
- Rotate image — algorithm in two steps?
- Spiral matrix — boundary trick?
- Pow(x, n) — algorithm?
- Single Number — pattern?
- Counting Bits 0..n — recurrence?
- Sum of Two Integers without
+— bit logic?
Saying it out loud. (The greedy caveat, said unprompted.) “Greedy is the pattern that looks right and is wrong, so let me justify it rather than assume it — with an exchange argument: if some optimal solution differs from my greedy choice, I can swap my choice in without making it worse. If I can’t make that argument, I’ll say so and fall back to DP.” For intervals: sort by start and sweep to merge, n log n dominated by the sort — but sort by end time for maximum non-overlapping, because finishing early leaves the most room. For bits, say what the identity does out loud: n AND n-minus-one clears the lowest set bit, XOR cancels pairs. And flag that Python ints are arbitrary precision, so 32-bit problems need an explicit mask.
Section O — Senior signals (Q156–165)
- Show me the 5-step problem-solving protocol.
- What do you do in the first 2-5 minutes of a problem?
- What if you’re stuck after 10 minutes?
- How do you state complexity properly?
- How do you test your code in an interview?
- How do you handle edge cases out loud?
- How do you communicate while coding?
- How do you ask clarifying questions without sounding lost?
- What’s a common bug pattern in your code that you watch for?
- How do you decide between brute force vs optimal in an interview?
Saying it out loud. (The whole script, in order.) “Let me make sure I understand — input’s X, I return Y, n goes to this. Duplicates? Negatives?” Then walk their example plus one of your own, ideally an empty one. Then: “Brute force is every pair, n squared — that’s correct but we can do better.” Then: “This looks like sliding window because the constraint is on a contiguous range, so O of n.” Then code while narrating the invariant, and finish by tracing a small input and restating time and space. The failure mode this whole script exists to prevent is going silent — the interviewer is scoring your reasoning, and a quiet candidate with working code routinely loses to a talkative one who didn’t finish.
Self-grading
- 130+ correct: ready for big-tech / frontier-lab coding rounds.
- 95–129: re-read the deep dive and drill weak patterns.
- 60–94: spend a week on full deep dive + 5 problems per weak pattern.
- <60: build pattern foundations from §2–§19; one section per day for two weeks.
8-week drill plan (mapped to NeetCode 150)
- Week 1: Arrays & Hashing, Two Pointers, Sliding Window. Drill A, B.
- Week 2: Stack, Binary Search, Linked List. Drill C, D, E.
- Week 3: Trees, Tries, Heap. Drill F, G, H.
- Week 4: Backtracking, Graphs. Drill I, J.
- Week 5: Advanced Graphs. Drill K.
- Week 6: 1D DP. Drill L.
- Week 7: 2D DP. Drill M.
- Week 8: Greedy, Intervals, Math, Bits + mock interviews. Drill N, O.
Daily: 1 problem solo (30 min) → check editorial → re-attempt next day from scratch.