Two pointers coding interview questions
Opposite-ends two pointers
Start at both ends of a sorted array or of a height map, and move the pointer that cannot improve the answer.
1.1 Two Sum II - Input Array Is Sorted Medium 3 min
The array is already sorted, so I put one pointer at each end. If the sum is too small I move the left pointer up, and if it is too large I move the right pointer down. I return the 1-based indices when the sum matches the target. That is linear time and constant extra space.
Read the full answer1.2 Container With Most Water Medium 3 min
I start with one pointer at each end of the height array. The area is the smaller of the two heights times the distance between them. I then move the pointer on the shorter side, because shrinking the width can only help if the next line is taller. I keep the best area. That is one pass and constant extra space.
Read the full answer1.3 Trapping Rain Water Hard 3 min
Water at an index is limited by the shorter of the tallest bars to its left and to its right. I walk from both ends and keep a running left max and right max. The side with the smaller max is already bounded by the other side, so I can add the trapped water there and move that pointer. That is linear time and constant extra space.
Read the full answer
Same-direction read/write pointers
A write index compacts the array in place, or two read pointers walk two sequences in order.
2.1 Merge Sorted Array Easy 3 min
nums1 has spare room at the end, so I fill it from the back. I compare the last unused values of nums1 and nums2 and write the larger one into the next free slot from the right. That way I never overwrite a value I still need, and leftover nums2 values copy across. Linear time and constant extra space.
Read the full answer2.2 Move Zeroes Easy 3 min
I keep a write index for the next non-zero position and scan from the left. Whenever I see a non-zero I swap it into the write index and advance that index, so the non-zero values keep their order and the zeros bubble to the end. One pass, constant extra space.
Read the full answer2.3 Remove Duplicates from Sorted Array Easy 3 min
Because the array is sorted, duplicates sit next to each other. I keep a write index for the next unique value and scan from the left. Whenever the read value differs from the last unique one I write it forward and advance the write index. I return that index as the new length. Linear time, constant extra space.
Read the full answer2.4 Is Subsequence Easy 2 min
I walk the longer string once and advance a pointer in the shorter string whenever the characters match. If that pointer reaches the end, every character of s appeared in order inside t. One pass over t and constant extra space.
Read the full answer
k-Sum
Sort, fix the first values, and finish the remaining pair with opposite-ends two pointers.
3.1 3Sum Medium 4 min
I sort the array, then fix one number and run the opposite-ends two-pointer search for a pair that sums to its negation. I skip duplicate values at the fixed index and at both pointers so each triplet appears once. Sorting costs n log n, and the two-pointer pass is quadratic, which is the expected bound.
Read the full answer