The Holy Grail of Problem Solving
315 problems. 28 patterns. 11 families. 8 primitive moves. One thinking process you'll keep for the rest of your career.
Built for Python. Built for FAANG and startups. Built for those who cannot memorize but can learn to recognize.
How to think (and remember)
Memorizing 500 problems will not save you if you cannot think on a new one. The goal is a reliable process that generates solutions from a small set of primitives.
The central insight: you are not trying to remember. You are trying to recognize. Your memory is not the bottleneck; your process for asking the right questions is.
The 7-question tree
- What is the input shape? Array, linked list, tree, graph, grid, intervals, stream.
- Is it sorted, or would sorting help? Sorted usually unlocks two pointers or binary search. Sorting often unlocks greedy.
- What is the local relationship? Pair sum, next greater, prefix difference, parent/child, neighbor cell, overlapping interval.
- What is the output? Single number, boolean, all solutions, top-k, shortest distance, merged structure.
- What do constraints imply?
n <= 20means backtracking or bitmask.n <= 100000means linear or sort.nhuge means logarithmic or math. - Where does brute force repeat work? Same pair, same window, same subproblem, same graph state, same prefix.
- Can a tiny example reveal the recurrence? Compute n=1,2,3 by hand and name what state changes.
Curiosity is a tool. After every problem, ask one why and one what-if: what if sorted, duplicates, streaming, weighted, mutable, or too large for memory?
The 60-second triage
Before writing a line, classify the input. The input signal usually tells you the pattern.
Pattern recognition trainer
The 8 primitive moves
Every solution is a composition of two or three of these. If memory is hard, memorize these eight.
HASH
Trade space for O(1) lookup. Two Sum's secret weapon.
SORT
Impose order to enable scans. Greedy's gateway.
TWO POINTERS
Two indices guided by an invariant.
SLIDING WINDOW
Maintain a valid range while sweeping.
BINARY SEARCH
Halve a sorted or monotonic space.
DFS / BFS
Explore reachable states. BFS for shortest unweighted.
HEAP
Top-K, running median, scheduling.
STACK
Defer decisions until context arrives.
11 problem families - connect the dots
Do not ask "have I seen this?" Ask "which family is this?" The family gives you the skeleton.
Exhaustive search
choose -> recurse -> unchooseSubsets, permutations, N-Queens, Combination Sum, Word Search.
Best so far ending here
dp[i] = f(dp[i-1], dp[i-2])Kadane, House Robber, Decode Ways, Climbing Stairs.
Match two sequences
dp[i][j] on two prefixesLCS, Edit Distance, Regex, Wildcard, Distinct Subsequences.
Fill a knapsack
pick item or skip itemPartition Equal Subset, Target Sum, Coin Change.
Partition intervals
dp[l][r] = best split at kBurst Balloons, Matrix Chain, Cut a Stick.
Sort then sweep
sort, maintain running stateIntervals, meetings, arrows, activity selection.
Top K / running order
heap size k or two heapsK closest, median stream, merge K lists.
Next greater / parens
monotonic stackDaily temperatures, histogram, valid parentheses.
Graph reachability
DFS/BFS with visitedIslands, clone graph, flood fill, surrounded regions.
Graph ordering
topological sortCourse Schedule, Alien Dictionary, Parallel Courses.
Shortest / cheapest path
BFS / Dijkstra / Bellman-FordNetwork Delay, Cheapest Flights, Min Effort Path.
Interview playbook
Interviewers score problem solving, code quality, and communication. Staying silent is usually what loses communication points.
Phase 1 - Clarify
"Input is ___, return ___. Can it be empty? Duplicates? Sorted? Value range? Time vs space priority?"
Phase 2 - Examples and brute force
"The naive approach is ___, O(___). Let me find where it repeats work."
Phase 3 - Pattern identification
"I notice ___, which suggests ___."
Phase 4 - Algorithm before coding
"I will maintain ___; the invariant is ___; complexity is ___."
Phase 5 - Code
"I will handle edge cases first, then write the invariant-heavy loop carefully."
Phase 6 - Trace and test
"Let me walk the example and then check empty, single element, duplicates, and boundary values."
Phase 7 - Complexity and tradeoffs
"Time is ___ because ___. Space is ___. An alternative would trade ___ for ___."
How problems twist
Interviewers take a base problem, then twist one knob. Recognize the knob and the new problem becomes familiar.
The pattern mindmap
Everything reduces to a small core: eight primitive moves, a ring of patterns, and families of variations.
How to assimilate this. Read by clusters: orange means sweep, teal means structures, blue means traverse states, purple means enumerate choices, red means cache overlapping subproblems. Memorize the clusters; the details rebuild themselves through practice.
The night-before summary
If you only remember this section, you can still do well.
Brute force first
Earns points, buys thinking time, reveals repeated work.
Name the pattern
Say it before coding so the interviewer can steer you early.
Trace one example
Catches most bugs and demonstrates rigor.
- What is the input shape? Array, tree, graph, intervals, grid, stream.
- What does the constraint size allow? Backtracking, quadratic, sort, linear, log.
- Which primitives combine here? Usually two or three.
- Which family does this belong to? Retrieve the skeleton, not a memorized answer.