
First Missing Positive
The array is not just input. Under the right invariant, it becomes its own presence map.
View solutionAlgorithms whose central requirement or advantage is transforming an input structure in place while preserving required data, ordering, reachability, or region invariants.
Tagged articles
24 articles in this tag.

The array is not just input. Under the right invariant, it becomes its own presence map.
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
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 common failure mode here is treating linked lists like arrays: copy the values, sort them, and rebuild. That throws away the structure the problem…
View solution
The reliable way to solve Next Permutation is to stop thinking in four memorized steps. Read the suffix, identify where it is already maximal, then make…
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
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 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 binary search tree has a structural invariant that is more useful than its parent-child relationships: inorder traversal visits values in sorted order.…
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 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 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
When a problem says “remove duplicates,” the first instinct is often to reach for a set. That works for an unsorted list, but it misses the key clue here:…
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
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
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
Reversing a linked-list segment is easy. Preserving everything on both sides of that segment is the real interview problem.
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
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
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 phrase “try digits and backtrack” is the easy part. The interview-grade solution keeps four representations synchronized: the board, row constraints,…
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