Two pointers, sliding window, fast and slow pointers, binary search variants, backtracking, monotonic stacks and bit manipulation tricks.
45 cards · basic cards · AI-written, checked twice. Edit anything.
- Two-pointer technique definition
Show answer
Use two pointers starting from different positions to solve array or list problems in O(n) time
- When is two-pointer approach useful?
Show answer
When finding pairs, removing elements, or comparing values in a sorted array in a single pass
- How do pointers move in a sorted array two-pointer problem?
Show answer
If sum is too large, move right pointer left; if too small, move left pointer right
- What does convergence mean in two-pointer?
Show answer
The left and right pointers move toward each other until they meet
- Two-pointer example: find pair with target sum
Show answer
Place left pointer at start, right at end; move them based on whether current sum is less than or greater than target
- How to handle duplicates in two-pointer approach?
Show answer
Skip duplicate values by advancing pointer past all equal elements
- Time complexity advantage of two-pointer
Show answer
Reduces time from O(n^2) brute force to O(n) with only one or two passes through data
- Reverse array in place with two pointers
Show answer
Start pointers at both ends, swap elements, move toward center until they meet
- Sliding window technique definition
Show answer
Maintain a window of consecutive elements that expands and contracts based on a condition
- When to use sliding window?
Show answer
For subarray or substring problems where you need to find minimum, maximum, or optimal window matching a condition
- Expand-contract pattern in sliding window
Show answer
Expand window by moving right pointer until condition breaks, then contract by moving left pointer to restore condition
- Common sliding window problems
Show answer
Longest substring without repeating, minimum window substring, maximum sum subarray of fixed size
- Data structure for tracking sliding window state
Show answer
Hash map to track character frequency or set to track distinct elements in current window
- Time complexity of sliding window
Show answer
O(n) because each element is visited at most twice, once by right pointer and once by left pointer
- Find longest substring without repeating characters
Show answer
Use sliding window with hash map tracking last seen index; move left pointer when duplicate found