Micro-questions over the two pointers pattern. Answer in a full sentence before opening the toggle. Format explained in the arrays bank.
A. Why it exists
1. What is the naive cost of finding a pair summing to a target?
Answer
— for each element, loop through all later ones. 500,000 checks for 1,000 transactions.
2. Give the party analogy.
Answer
Start with the person who has the least money and the person who has the most, and based on whether their combined total is too much or too little, move one of them inward.
3. Define the pattern.
Answer
Walk two positions through a sorted structure at once, letting their relative movement do the work a nested loop would otherwise do.
4. What does it turn into, and under what conditions?
Answer
— when the data is sorted and you need pairs (or a small fixed number of elements) satisfying a condition on their sum or difference.
B. The mechanism
5. Where do the two pointers start?
Answer
left = 0 and right = len(nums) - 1 — the two ends.
6. State the movement rule.
Answer
Sum too small → move left up (need a bigger element). Sum too large → move right down (need a smaller one).
7. Trace [1,2,3,4,6], target 6.
Answer
1+6=7 > 6 → right--; 1+4=5 < 6 → left++; 2+4=6 → found indices [1, 3].
8. Why does this work without checking every pair?
Answer
At each step one side of the comparison is eliminated entirely, not just one pair — moving left up rules out every pair that still includes the old, too-small left value paired with anything.
9. What is convergence here?
Answer
The two pointers moving toward each other until they meet or cross, which is the loop’s termination condition.
C. Extensions
10. How does three-sum use the pattern?
Answer
Fix one element with an outer loop, then two-pointer the rest — overall after the sort.
11. How do you avoid duplicate triples?
Answer
Skip repeated values at the outer index (if i > 0 and nums[i] == nums[i-1]: continue), and after a hit, advance both pointers past any equal neighbours.
12. Container with most water — what is the area formula?
Answer
.
13. Which pointer moves, and why is that correct?
Answer
The shorter line moves inward. Moving the taller one cannot help: the width shrinks and the height is still capped by the shorter line, so every such pair is strictly worse.
14. Is the array sorted in container with most water?
Answer
No — which is worth noticing. The pattern here is driven by a monotone elimination argument, not by sortedness.
D. Trade-offs
15. What does the sorting requirement cost?
Answer
— which may be more expensive than the pair check for small .
16. What is the alternative for unsorted data?
Answer
A hash-map version — time but space, losing the space efficiency that is two pointers’ main advantage.
17. How far does the pattern extend?
Answer
To three-sum, four-sum and so on — but the complexity grows with the number of pointers, roughly for -sum.
18. What conditions does it suit, and which does it not?
Answer
Sum and difference conditions. For product or ratio conditions the pattern may not apply directly, because the monotonicity argument breaks.
19. Do duplicates break the basic algorithm?
Answer
No — it still finds a valid pair. Duplicates only matter when the problem asks for distinct results, which is what the skip-duplicates logic is for.
20. Summarise the pattern in one sentence.
Answer
Start at both ends and move the pointer that can still improve things — each step eliminates a whole side, which is why a nested loop collapses to one pass.
Scorecard
| Pass | Date | Got it | Close | Blank | Questions missed |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 |
Related
- Two Pointers — the pattern
- Sliding Window — Question Bank — two pointers moving the same direction
- Fast & Slow Pointers — Question Bank — two pointers at different speeds
Software engineering
Infrastructure & security
Computer science
Hardware & engineering