
3Sum Closest
The target does not identify the winning triplet. It tells each pointer which direction is still worth exploring.
View solutionArticles whose authoritative difficulty is intermediate.
Tagged articles
62 articles in this tag.

The target does not identify the winning triplet. It tells each pointer which direction is still worth exploring.
View solution
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 lists already expose digits in the order addition needs. Scan both lists together, track one carry, and keep going until there is no digit or carry…
View solution
The output is bottom-up, but the traversal should still be top-down: collect ordinary BFS levels, then reverse the completed groups.
View solution
If you visit every node but mix adjacent depths, the traversal is still wrong. The key is to make the queue represent one level at a time.
View solution
The queue does not need to zigzag. It only needs to preserve a predictable order while we place each value into its correct output position.
View solution
The hard part is not finding combinations that add to the target. It is finding them once while respecting the physical number of occurrences in the input.
View solution
Treat this as an enumeration problem, not a permutation problem. Sort the candidates, keep combinations in nondecreasing order, recurse from the same index…
View solution
The duplicate-ordering trap is the whole problem: [1, 2] and [2, 1] represent one selection, not two. Build every path in increasing order, and the…
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
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 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
A sorted array gives you the middle value immediately. A singly linked list makes you walk for it. The better solution avoids the walk entirely: let…
View solution
The Count and Say solution is a repeated state transition: start with "1", scan the current string into maximal consecutive runs, and emit each run as…
View solution
The recurrence resembles Fibonacci, but zeros can remove transitions entirely. Derive the valid-token transitions first; the dynamic program then follows…
View solution
A standard binary search finds a match. This problem asks for the entire matching block. The difference is one boundary decision.
View solution
It places the left subtree in the correct position, but it can disconnect the original right subtree. The real task is to move the left subtree in front of…
View solution
Generate only prefixes that can still become valid. The balance state tells you exactly which branches to keep.
View solution
Gray code looks like a permutation problem, but the useful signal is more specific: enumerate every n-bit state so that each move flips exactly one bit,…
View solution
A useful Group Anagrams solution does not compare every string with every existing group. It assigns each string a stable identity based on its character…
View solution
The common trap in the Insert Interval solution is treating the task as generic sorting followed by generic interval merging. That works, but it ignores…
View solution
The common mistake in a Jump Game II solution is to commit too early: “From this index, which landing position should I choose?” That creates a path-search…
View solution
The wrong mental model is a tree of jump paths. The useful model is a moving boundary: the farthest index reachable by any valid path found so far.
View solution
The difficult part is not recognizing a palindrome. It is preserving contiguity while avoiding repeated work.
View solution
A repeated character is not a reason to restart the scan. It is a reason to move the left boundary to the first position that makes the current window…
View solution
Resetting a running sum to zero looks like the obvious solution—until the array contains only negative numbers. Then the algorithm can quietly return an…
View solution
Pairwise merging feels natural until one merge creates a new overlap with a third interval. Then the bookkeeping branches, earlier comparisons become…
View solution
The right Minimum Path Sum solution is a two-dimensional dynamic program. For every coordinate, store the minimum sum needed to reach it from the top-left.…
View solution
Treat the product as a fixed array of decimal positions. Every digit pair has a predictable destination; carry normalization keeps those positions valid.
View solution
A linked-list partition fails in one of two ways: it loses the unread suffix, or it preserves a stale link and creates the wrong structure. The reliable…
View solution
That is the central trap in Path Sum II. An internal node may bring the running sum to targetSum, but its path is still incomplete if the node has a child.…
View solution
When nums = [1, 1, 2], ordinary permutation backtracking treats the two 1 values as different input positions. That creates duplicate value sequences.
View solution
The search tree is easy to recognize and easy to corrupt. Build one position at a time, choose an unused value, recurse, then undo exactly that choice.
View solution
The queue solution is easy to derive. The constant-space solution comes from reusing the next pointers you are building as the queue for the next level.
View solution
The queue-based solution is easy to see. The constant-space solution is easier to miss: once one level is connected, its next pointers become the queue for…
View solution
A linear multiplication chain follows the definition of a power. It also ignores the only fact that matters at interview scale: the exponent can be halved.
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 key distinction is easy to miss: this problem removes every node belonging to a repeated value. It does not keep the first occurrence. The solution is…
View solution
The target is named from the end, but a singly linked list only lets you move forward. The key move is to convert that backward-looking position into a…
View solution
The trap is to think “place three dots.” The useful model is narrower: choose exactly four contiguous digit segments, validate each one immediately, and…
View solution
Reversing digits is easy. Reversing them without ever creating an unsafe intermediate value is the interview problem.
View solution
A localized reversal fails at the boundaries: the middle looks correct, but the prefix disappears, the suffix becomes unreachable, or the returned head is…
View solution
The hard part of rotating a matrix is not visualizing the turn. It is moving every value without destroying one that has not moved yet.
View solution
A right rotation looks repetitive when described one node at a time. The useful implementation is one split, one reconnection, and one cut.
View solution
A matrix can be two-dimensional storage with a one-dimensional search space. Prove that shape first, then run ordinary binary search over virtual indices.
View solution
Duplicates turn a clean binary-search decision into an information problem. When nums[left], nums[mid], and nums[right] are equal, you cannot tell which…
View solution
Rotation breaks global order, not all order. Find the sorted half, test its value range, and discard what that range proves impossible.
View solution
The dangerous part is not writing zeroes. It is remembering which zeroes were causes and which zeroes were created by your own writes.
View solution
Treating this as string cleanup is how you lose the problem. Removing every dot breaks valid names such as ..., while careless parent handling can let /../…
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 key distinction is simple: Spiral Matrix reads values from an existing grid; Spiral Matrix II constructs the grid while the spiral advances. The…
View solution
A spiral traversal can look correct on a square matrix and still fail immediately on a single row or column. The reliable model is a shrinking rectangle:…
View solution
A reliable atoi parser is a small state machine: skip leading spaces, read one optional sign, consume one numeric prefix, and guard every accumulator…
View solution
The duplicate bug comes from treating equal input positions as different decisions. Sort first, then skip equal candidates only when they are siblings at…
View solution
A value swap can produce the right sequence while violating the contract. The real task is to move node identities by changing links—and to do it without…
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
The recurrence is familiar. The interview usually turns on the cells you forgot: a blocked start, a blocked destination, or an obstacle that permanently…
View solution
The reliable way to solve Unique Paths is to count paths to each cell, not to enumerate complete routes. Every cell has at most two meaningful…
View solution
A Sudoku validator does not solve the puzzle. It tracks whether the digits already placed violate any row, column, or 3×3 box constraint.
View solution
A BST validator must remember the ancestors that still constrain the current node.
View solution
A grid DFS can match the right letters and still be wrong. The missing piece is path-local state: mark a cell when you enter it, explore from that choice,…
View solution
A visual zigzag is easy to draw and surprisingly easy to implement incorrectly. The reliable solution is smaller: track the current row, track the movement…
View solution