Linear patterns · core
Two pointers
Two pointers walk a sorted array or a string from both ends (or at two speeds), moving the pointer that can improve the answer, so a pair search drops from O(n²) to O(n) with O(1) extra space.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Opposite ends on sorted input: sum too small, move the left pointer up; too big, move the right pointer down. Each step removes one candidate, so it is O(n).
- Same direction (read and write): one pointer reads, one writes the next kept value; remove duplicates, move zeros, compact an array in place.
- Fast and slow pointers: find the middle, detect cycles (Floyd), find the k-th from the end.
- 3Sum = sort, fix i, then two pointers on the rest: O(n²). Skip duplicates at i, and after a match skip equal values at both pointers.
- Palindrome checks: compare s[l] and s[r], skipping characters that are not letters or digits and ignoring case.
- If the input is not sorted, sorting first costs O(n log n) and loses the original indices; a hash map may be better when indices are needed.
The red pen
Common traps: mistake → fix
Mistake: Two pointers work on any array
Fix: the move rule needs sorted data (or a palindrome-like symmetry).
Mistake: 3Sum duplicates can be removed with a set of triplets at the end
Fix: it works but hides the O(n²) skip logic interviewers expect.
Mistake: After a match you can move only one pointer
Fix: move both, then skip equal values.
Easy to hard
Questions interviewers ask, easy to hard
- Find a pair with a target sum in a sorted array, O(1) extra space.
- Remove duplicates from a sorted array in place.
- 3Sum with no duplicate triplets. Why can hashing not beat O(n²) here?
- Valid palindrome ignoring punctuation and case.
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.