Skip to content
Tech Interview Prep
Technical interview guide

Two Pointers

Two indices moving through a sequence, from opposite ends or in lockstep, to cut brute-force O(n²) scans to O(n).

Read
40 min
Practice MCQs
25
Interview Q&A
25
Edition
v7
Editorial status
Reviewed

Scope: Language-neutral, with operation costs cited from CPython 3.14 and the C++ standard containers.

Curated: · Written: · Reviewed:

25 questions

MCQ-17A service intersects two sorted ID lists, a = [2, 4, 6, 8] and b = [4, 5, 6, 7], with one pointer per list: on a mismatch advance the pointer whose head is smaller, on equality emit the value and advance both, and stop when either pointer runs off the end. What does it return?
MCQ-25A candidate scans the unsorted array [1, 9, 2, 8, 3, 7] for two elements summing to 10 with the standard two-pointer sweep: i = 0, j = 5, advance i when a[i] + a[j] < 10 and advance j when the sum is greater. The scan reports no pair, though 1 + 9 = 10. What is the correct diagnosis?
MCQ-1A candidate implements two-sum on the sorted array a = [2, 5, 8, 9, 12, 17] with target = 20: ```python lo, hi = 0, len(a) - 1 while lo < hi: s = a[lo] + a[hi] if s == target: return lo, hi if s < target: lo += 1 hi -= 1 else: hi -= 1 return None ``` The function returns None, but 8 + 12 = 20 is in the array. Which diagnosis explains the miss?
MCQ-2A pair-sum function walks a sorted array from both ends: lo = 0, hi = n-1; while lo < hi it compares a[lo] + a[hi] with the target, advancing lo when the sum is below the target and retreating hi when it is above, and returns the first pair whose sum equals the target. It is called with a = [1, 4, 4, 7, 10] and target 8. What does it return?
MCQ-7A sorted array `a = [1, 1, 2, 2, 3, 3]` is scanned with `lo = 0` and `hi = 5` to report every distinct pair summing to 4. On a match the loop emits the pair and steps both pointers inward one index; otherwise it moves lo up when the sum is too small and hi down when the sum is too large. A test asserts that the emitted list is exactly `(1, 3)` then `(2, 2)` and that `a[lo] + a[hi]` is evaluated exactly twice. Which adjustment after each emission makes that test pass?
MCQ-9A candidate runs the two-pointer maximum-area algorithm on heights = [2,5,1,4,3]. The loop starts with lo=0, hi=4. What maximum area does it return?
MCQ-16Given an elevation array, the two-pointer trapping-rain-water solution moves left and right pointers inward. Which statement correctly describes why the pointer on the side with the smaller current height can be advanced, and how much water is added at that step?
MCQ-13A signup service checks whether a submitted handle is a palindrome, ignoring spaces and letter case. The deployed function is: ```python def is_palindrome(s): lo, hi = 0, len(s) - 1 while lo < hi: while lo < hi and s[lo] == ' ': lo += 1 if s[lo].lower() != s[hi].lower(): return False lo += 1 hi -= 1 return True ``` A user submits the handle `'abba '`: the five characters a, b, b, a, space. What does the validator return?
MCQ-3In an interview on Floyd's cycle-finding algorithm, you are asked which invariant guarantees that the fast pointer meets the slow pointer whenever the list contains a cycle. Which of these statements is that invariant?
MCQ-10During a technical interview on Floyd's cycle-finding algorithm, a candidate is asked why advancing one pointer from the list head and one pointer from the collision node at equal speed guarantees meeting at the cycle entrance. Which explanation is mathematically sound?
MCQ-12A `middleNode` helper must return the second of the two middle nodes on an even-length list: for 1→2→3→4 it must return the node holding 3. A developer writes: ```python slow = fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next return slow ``` For the input 1→2→3→4, what does this return, and does it meet the spec?
MCQ-11A single left-to-right pass compacts an array in place: a read pointer examines each element, and a write pointer advances only when an element is kept and copied to it. Which statement describes the array at every step of that pass?
MCQ-4A two-pointer routine is written to compact a sorted array in place: `write` starts at 1, and for each `read` from 1 to n-1 it copies `a[read]` to `a[write]` and increments `write` only when `a[read] != a[write-1]`, then returns `write`. A teammate runs it on `[2, 2, 3, 1, 3]`, which is not sorted. What does the call return, and what does the array hold afterward?
MCQ-14A stable sort's merge step combines two runs of (key, label) records. Run A is [(1, 'A1'), (3, 'A2'), (3, 'A3')] and run B is [(2, 'B1'), (3, 'B2'), (4, 'B3')]. The merge is: ```python i = j = 0 out = [] while i < len(A) and j < len(B): if A[i][0] <= B[j][0]: out.append(A[i]); i += 1 else: out.append(B[j]); j += 1 out += A[i:] out += B[j:] ``` Which sequence does `out` hold?
MCQ-15You are merging sorted b into sorted a in place, with a's spare slots at the end. On a = [4,5,6,0,0,0], b = [1,2,3], a forward merge from index 0 produces [1,1,1,2,3,6] instead of [1,2,3,4,5,6]. Which invariant does the back-to-front merge maintain to avoid this?
MCQ-21A binary search over the sorted array `[1, 2, 3, 4, 5, 6, 7]` returns -1 for `target = 1` and for `target = 7`, although both values are in the array. The code is: ```python def bsearch(a, target): lo, hi = 0, len(a) - 1 while lo < hi: mid = (lo + hi) // 2 if a[mid] == target: return mid elif a[mid] < target: lo = mid + 1 else: hi = mid - 1 return -1 ``` Which single change makes the function return the correct index for every target?
MCQ-5Six jobs are queued as `[A:urgent, B:normal, C:urgent, D:normal, E:urgent, F:normal]`. A downstream merge requires normal jobs to stay in their original relative order. You run the standard in-place two-pointer partition that scans left to right and swaps each urgent job into the growing urgent prefix. What is the resulting array, and what does it mean for the merge requirement?
MCQ-6You are tracing three-way partitioning (Dutch National Flag) on `[1, 2, 3, 1, 2]` with pivot value 2. The pointers sit at low = 1, mid = 1, high = 4, and `array[mid]` equals the pivot. Which pointer update does the algorithm perform for this element?
MCQ-8An implementation sorts the input and sweeps with lo = i + 1 and hi = N - 1. It must return each distinct triplet exactly once: including triplets that use the same value at two positions, such as [-1,-1,2] and [0,0,0]: in O(N^2) time and O(1) auxiliary space beyond the sort. Which pointer discipline satisfies all of that?
MCQ-24A teammate submits this scan and calls it O(n) because it uses two pointers: ```python def scan(a, target): n = len(a) iters = 0 for hi in range(n - 1, 0, -1): lo = 0 while lo < hi and a[lo] + a[hi] < target: lo += 1 iters += 1 return iters ``` For an array of n = 8 elements and a target larger than every pair sum, how many times does the inner body execute?
MCQ-18A log-join service matches 100,000 sorted query timestamps against a sorted array of 1,000,000 event timestamps. The array is a read-only memory-mapped file that must not be copied into RAM. Which lookup strategy fits this workload?
MCQ-19A pair-sum handler sorts a 10,000,000-element int32 array and then runs a two-pointer walk. Its p99 is 2.4 s against a 1.5 s budget. A profile attributes 2.3 s to the sort and 0.1 s to the walk. The team's plan is to optimize the walk. What should the SLO review conclude?
MCQ-22A service checks whether any two values in a sorted array of 50,000,000 int64s sum to a target. The array itself occupies 400 MB and the container is capped at 2 GB. One implementation keeps a hash index of the values already seen; the other walks two pointers inward from both ends. Both finish in about the same time, 100-140 ms. The hash version is OOM-killed during the check; the two-pointer version ends the check at the RSS it started with. Which account of that difference fits the numbers?
MCQ-23A nightly job keeps one list object, `events` (10,000 dicts), and passes it to `summarize(events)` and then to `render_timeline(events)`, which prints events in arrival order for a compliance export. `summarize` starts with `events.sort(key=lambda e: e['ts'])`. Unit tests build a fresh list per call and pass. In production the export prints events in timestamp order. Which change fixes the defect?
MCQ-20An API gateway logs request latencies in arrival order: `lat = [120, 45, 300, 45, 210]` (ms). A report must list the arrival positions (0-based) of the two slowest requests, slowest first. Which list of positions is correct?