
3Sum
A reliable 3Sum solution comes from turning a cubic search into a sequence of sorted two-sum scans—and proving why each pointer move is safe.
View solutionCoordinate two indices around a movement invariant to eliminate candidates efficiently in sorted arrays, strings, partitions, and ranges.
Problems
Practice problems that share this primary solution pattern and compare the clues that reveal it.

A reliable 3Sum solution comes from turning a cubic search into a sequence of sorted two-sum scans—and proving why each pointer move is safe.
View solution
The target does not identify the winning triplet. It tells each pointer which direction is still worth exploring.
View solution
Four choices suggest an O(n^4) search. Sorting changes the last two choices into a controlled walk.
View solution
The hard part is not calculating width × shorter_height. It is proving why one whole family of pairs can be discarded without checking them.
View solution
The word “remove” is misleading here. You do not need to shrink the Python list or delete values from its tail. You need to compact the distinct values…
View solution
The array is not shortened. The judge inspects only a prefix, so the job is to compact the values you keep into that prefix and return its length.
View solution
A left-to-right merge can overwrite values in nums1 before you have compared them. The reliable Merge Sorted Array solution uses backward two pointers:…
View solution
The trap is treating duplicate removal as a counting problem. The sharper model is an input stream and a compacted result prefix: read every candidate,…
View solution
The trap in the Sort Colors solution is assuming that “only three values” makes the problem trivial. Counting works. The interview version asks you to see…
View solution
The picture suggests moving inward from both ends. The proof requires more: finalize only the side whose limiting boundary is already certified.
View solution
The trap in this problem is stopping at “preorder gives the root.” That identifies one node, but not the boundaries of its children. The real solution is…
View solution
The root is easy to identify. The difficult part is preserving the remaining traversal state while the tree splits into subtrees.
View solution
Choosing the first or last value as the root preserves sorted order, but it produces a one-sided chain. The key is to choose a root that splits the…
View solution
The output looks like mathematics. The interview task is simpler: build each row from the row you already have.
View solution
The requested output is one row, not the entire triangle. Build that row with one working list, and update it from right to left so each calculation still…
View solution
The trap is visible: every row offers two choices, so it is tempting to choose the smaller child or enumerate every path. The reliable Triangle solution…
View solution