Average of Levels: Aggregating Inside the Snapshot
Replace the level list with a running sum — the aggregation variant. Python solution and complexity analysis for the BFS interview pattern.
Theme
FAANG-style coding interview prep — real questions with Python solutions, organized by the nine patterns that cover most of what interviewers ask.
100 articles
Replace the level list with a running sum — the aggregation variant. Python solution and complexity analysis for the BFS interview pattern.
Compare typed strings with backspaces in O(1) space, scanning backwards. Python solution and complexity analysis for the two pointers interview pattern.
Max profit in one pass by tracking the running minimum buy price. Python solution and complexity analysis for the sliding window interview pattern.
The template everyone thinks they know — invariant included. Python solution and complexity analysis for the modified binary search interview pattern.
Bottom-up levels — resist cleverness, reverse at the end. Python solution and complexity analysis for the BFS interview pattern.
The level-snapshot queue loop that eight other questions reuse verbatim. Python solution and complexity analysis for the BFS interview pattern.
Any-to-any max path — clamp negative arms to zero and track the bend. Python solution and complexity analysis for the DFS interview pattern.
What you see from the right — take each level's final node. Python solution and complexity analysis for the BFS interview pattern.
Alternate level direction with a flag — never touch the queue order. Python solution and complexity analysis for the BFS interview pattern.
Minimum ship capacity via a greedy day-splitting check. Python solution and complexity analysis for the modified binary search interview pattern.
Capacity checking with +passengers/−passengers events on a number line. Python solution and complexity analysis for the merge intervals interview pattern.
Cycle detection with direction rules and path invalidation. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Fibonacci in costume — the recurrence-first discipline in miniature. Python solution and complexity analysis for the dynamic programming interview pattern.
Fewest coins to a target — and why greedy dies on [1, 3, 4]. Python solution and complexity analysis for the dynamic programming interview pattern.
Maximize trapped area and prove the greedy pointer move is safe. Python solution and complexity analysis for the two pointers interview pattern.
Track depth and parent per node — two facts decide cousinhood. Python solution and complexity analysis for the BFS interview pattern.
One-pass middle deletion — track the node before the midpoint. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Return one thing, track another — the two-quantity DFS shape. Python solution and complexity analysis for the DFS interview pattern.
Levenshtein distance — three operations, one table. Python solution and complexity analysis for the dynamic programming interview pattern.
Common free intervals across k schedules — flatten, merge, read the gaps. Python solution and complexity analysis for the merge intervals interview pattern.
Every anagram start index in one pass — the counting version of inclusion. Python solution and complexity analysis for the sliding window interview pattern.
No mutation, O(1) space — the array is secretly a cyclic list. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Range of a duplicated target via lower and upper bound in O(log n). Python solution and complexity analysis for the modified binary search interview pattern.
The two-heap balance: max-heap low half, min-heap high half. Python solution and complexity analysis for the heap and top-k interview pattern.
Converge on the rotation seam via right-edge comparison. Python solution and complexity analysis for the modified binary search interview pattern.
Climb the slope — halving works even on unsorted data. Python solution and complexity analysis for the modified binary search interview pattern.
Google's storytelling version of longest subarray with two distinct values. Python solution and complexity analysis for the sliding window interview pattern.
Floyd's algorithm on an implicit sequence — no nodes required. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Non-adjacent maximum sum — the two-variable rolling DP. Python solution and complexity analysis for the dynamic programming interview pattern.
Exploit existing sortedness: before, absorb, after — three phases. Python solution and complexity analysis for the merge intervals interview pattern.
Find where two lists merge by switching heads at the ends. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Intersect two sorted interval lists with merge-style pointers. Python solution and complexity analysis for the merge intervals interview pattern.
The famous swap-children recursion — and its iterative twin. Python solution and complexity analysis for the DFS interview pattern.
Track the farthest reachable index — O(n) with one integer of state. Python solution and complexity analysis for the dynamic programming interview pattern.
Bounded max-heap on squared distance — monotone transforms are free. Python solution and complexity analysis for the heap and top-k interview pattern.
Explore an implicit sorted matrix — seed one row, expand neighbors lazily. Python solution and complexity analysis for the heap and top-k interview pattern.
Search on eating speed with a ceiling-division check. Python solution and complexity analysis for the modified binary search interview pattern.
The bounded min-heap of size k — and when quickselect beats it. Python solution and complexity analysis for the heap and top-k interview pattern.
Design a class where the size-k min-heap is the entire state. Python solution and complexity analysis for the heap and top-k interview pattern.
Repeatedly smash the two heaviest — negation makes heapq a max-heap. Python solution and complexity analysis for the heap and top-k interview pattern.
Detect a cycle in O(1) space and argue why the pointers must meet. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Return the node where the cycle starts — and prove the reset trick. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Match extends the diagonal, mismatch takes the better drop. Python solution and complexity analysis for the dynamic programming interview pattern.
The tails array and patience sorting — binary search inside DP. Python solution and complexity analysis for the dynamic programming interview pattern.
Replace k chars to maximize a repeat run — with a window that never shrinks. Python solution and complexity analysis for the sliding window interview pattern.
The most-asked string question: variable window with a last-seen map. Python solution and complexity analysis for the sliding window interview pattern.
Return what you found — the split node emerges from the recursion. Python solution and complexity analysis for the DFS interview pattern.
Longest run of ones after flipping at most k zeros — count, never flip. Python solution and complexity analysis for the sliding window interview pattern.
The fixed-window template: slide a length-k sum in O(1) per step. Python solution and complexity analysis for the sliding window interview pattern.
The four-line recursion that teaches the subtree-answer discipline. Python solution and complexity analysis for the DFS interview pattern.
Extend or restart — the ending-here state that makes it one pass. Python solution and complexity analysis for the dynamic programming interview pattern.
The O(log min(m, n)) partition argument, demystified. Python solution and complexity analysis for the modified binary search interview pattern.
Can one person attend every meeting? Sort and scan adjacent pairs. Python solution and complexity analysis for the merge intervals interview pattern.
Peak concurrent meetings — the min-heap of end times, plus the sweep line. Python solution and complexity analysis for the merge intervals interview pattern.
Collapse overlapping intervals — the question every FAANG loop reuses. Python solution and complexity analysis for the merge intervals interview pattern.
K-way merge in O(n log k) — with the tuple tie-break heapq requires. Python solution and complexity analysis for the heap and top-k interview pattern.
Find the midpoint without counting — the half-speed invariant. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Group intervals sharing a common point — activity selection inverted. Python solution and complexity analysis for the merge intervals interview pattern.
First leaf wins — early exit that full-tree DFS cannot match. Python solution and complexity analysis for the BFS interview pattern.
Shortest window reaching a target sum — shrink while you still can. Python solution and complexity analysis for the sliding window interview pattern.
Smallest window covering all of t, with the have-versus-need counter. Python solution and complexity analysis for the sliding window interview pattern.
Push all zeroes to the back in one pass, preserving order. Python solution and complexity analysis for the two pointers interview pattern.
Design a booking class — the two-sided overlap predicate under test. Python solution and complexity analysis for the merge intervals interview pattern.
Minimum removals to make intervals disjoint — sort by end, keep greedily. Python solution and complexity analysis for the merge intervals interview pattern.
The most-asked grid question — sink each island as you count it. Python solution and complexity analysis for the DFS interview pattern.
Palindrome check via midpoint split and half reversal. Python solution and complexity analysis for the fast and slow pointers interview pattern.
Equal-halves split — subset-sum with the backward loop. Python solution and complexity analysis for the dynamic programming interview pattern.
Collect all matching root-to-leaf paths — append, recurse, pop. Python solution and complexity analysis for the DFS interview pattern.
Root-to-leaf target sums — subtract as you descend, decide at leaves. Python solution and complexity analysis for the DFS interview pattern.
Detect an anagram window in O(n) with an incremental match counter. Python solution and complexity analysis for the sliding window interview pattern.
Wire each node to its right neighbor using the wires you just installed. Python solution and complexity analysis for the BFS interview pattern.
Dedupe a sorted array in place with a reader and a writer index. Python solution and complexity analysis for the two pointers interview pattern.
Delete the n-th from the end in one pass with a dummy head. Python solution and complexity analysis for the fast and slow pointers interview pattern.
L0-Ln-L1-Ln-1 interleaving via split, reverse, and merge. Python solution and complexity analysis for the fast and slow pointers interview pattern.
No two equal neighbors — alternate the two most frequent letters. Python solution and complexity analysis for the heap and top-k interview pattern.
Seed the queue with every rotten orange — simultaneous spread, counted in rounds. Python solution and complexity analysis for the BFS interview pattern.
Walk two trees in lockstep — the base-case ordering does the work. Python solution and complexity analysis for the DFS interview pattern.
One binary search over rows*cols with divmod index mapping. Python solution and complexity analysis for the modified binary search interview pattern.
The lower_bound boundary template every variant builds on. Python solution and complexity analysis for the modified binary search interview pattern.
The rotated classic — decide which half is sorted, then range-check. Python solution and complexity analysis for the modified binary search interview pattern.
Unweighted shortest path — BFS by definition, eight neighbors, mark early. Python solution and complexity analysis for the BFS interview pattern.
Window max in O(n) — the monotonic deque, explained properly. Python solution and complexity analysis for the sliding window interview pattern.
Rebuild a string most-frequent-first — full ordering, so no bounded trick. Python solution and complexity analysis for the heap and top-k interview pattern.
Three-way partition of 0s, 1s and 2s in one pass, no counting. Python solution and complexity analysis for the two pointers interview pattern.
First search-on-the-answer: largest k with k*k at most x. Python solution and complexity analysis for the modified binary search interview pattern.
Square a sorted array with negatives and stay O(n), no re-sort. Python solution and complexity analysis for the two pointers interview pattern.
Turn a sorted array into a->b range strings — clean run detection. Python solution and complexity analysis for the merge intervals interview pattern.
Check mirror symmetry by walking outer-outer and inner-inner pairs. Python solution and complexity analysis for the DFS interview pattern.
CPU cooldown scheduling — the frame formula and when idle time vanishes. Python solution and complexity analysis for the heap and top-k interview pattern.
Nearest triplet sum to a target, converging on the smallest gap. Python solution and complexity analysis for the two pointers interview pattern.
All unique triplets summing to zero — dedupe without a set of tuples. Python solution and complexity analysis for the two pointers interview pattern.
Counter plus bounded heap — and the bucket-sort answer that beats it. Python solution and complexity analysis for the heap and top-k interview pattern.
The classic hard: O(1)-space water trapping via boundary maxima. Python solution and complexity analysis for the two pointers interview pattern.
Find a pair summing to target in one pass and O(1) space. Python solution and complexity analysis for the two pointers interview pattern.
Paths through a lattice — 2D recurrence, 1D memory. Python solution and complexity analysis for the dynamic programming interview pattern.
Palindrome check with one allowed deletion — branch only on mismatch. Python solution and complexity analysis for the two pointers interview pattern.
Check a messy string is a palindrome without building a filtered copy. Python solution and complexity analysis for the two pointers interview pattern.
The local-check trap, defeated by passing (low, high) down the tree. Python solution and complexity analysis for the DFS interview pattern.
Can the string be segmented? Prefix reachability with a set lookup. Python solution and complexity analysis for the dynamic programming interview pattern.
Shortest transformation via wildcard buckets, not pairwise comparison. Python solution and complexity analysis for the BFS interview pattern.