Learn the vocabulary of two pointers converging across a sorted array instead of checking every pair.
0 / 5 completed
1 / 5
At standup, a dev mentions solving a problem on a sorted array by starting one pointer at the beginning and another at the end, moving them toward each other based on a comparison, instead of checking every possible pair of elements. What is this technique called?
The two-pointer technique starts one pointer at the beginning and another at the end of a sorted array, moving each one inward based on comparing the values at the two positions, which lets it find a matching pair, if one exists, in a single linear pass instead of checking every possible pair. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This convergent-pointer movement is exactly what avoids the quadratic cost of comparing every element against every other element.
2 / 5
During a design review, the team relies on the array being sorted first, specifically so the two-pointer technique can decide which pointer to move based on whether the current pair's sum is too high or too low. Which capability does sorting provide here?
Sorting the array first provides a reliable rule for which pointer to move next, since once the array is sorted, moving the left pointer forward is guaranteed to increase the pair's sum and moving the right pointer backward is guaranteed to decrease it, which is exactly what lets the technique decide deterministically which pointer to move at each step. Leaving the array unsorted would remove that guarantee entirely, since moving either pointer could move the sum in either direction unpredictably. This dependence on sorted order is exactly why the two-pointer technique is typically applied only after an explicit sort step, or on data that's already known to be sorted.
3 / 5
In a code review, a dev notices a solution applies the two-pointer technique to find a pair summing to a target value on an array that was never sorted first. What does this represent?
This is a broken precondition, since the two-pointer technique's entire rule for deciding which pointer to move relies on the array already being in sorted order, and applying it to an unsorted array means moving a pointer no longer reliably increases or decreases the pair's sum in a predictable direction. A cache eviction policy is an unrelated concept about discarded cache entries. This missing sort step is exactly the kind of precondition a careful reviewer checks for, since the code can look correct at a glance while silently producing wrong answers on unsorted input.
4 / 5
An incident report shows a service's pair-matching feature returned incorrect results in production, because the two-pointer technique was applied to an array of values pulled directly from a database query with no guaranteed order, and the array was never explicitly sorted first. What practice would prevent this?
Explicitly sorting the array before applying the two-pointer technique guarantees the precondition the technique depends on actually holds, restoring its ability to decide reliably which pointer to move at each step, which is exactly the fix for the incorrect results described in this incident. Continuing to apply the technique to the array exactly as it comes back from a database query, with no guaranteed order, is exactly what let the pair-matching feature silently return wrong results. This explicit sort step is a standard, necessary precondition whenever the input's order isn't already guaranteed by an earlier step in the pipeline.
5 / 5
During a PR review, a teammate asks why the team pays the cost of an explicit sort before running the two-pointer technique instead of just checking every possible pair directly with a nested loop on the unsorted array. What is the reasoning?
A nested loop over every possible pair costs quadratic time regardless of whether the array is sorted, since it compares every element against every other element without exception. Sorting once and then running the two-pointer technique instead costs the sort's own time, typically log-linear, plus only a single additional linear pass to find the pair, which for a large array is substantially cheaper overall than the nested loop's quadratic cost. The tradeoff is that this approach only pays off when the array is large enough that the sort's one-time cost is worth it, and it assumes the problem doesn't need to preserve the array's original, unsorted order for some other purpose.