DSA interview question
A ticketing system issued seat numbers in increasing order, but some numbers were skipped. Given the sorted list of issued numbers, the box office wants the k-th smallest positive number that was never issued, to reuse it. Walking up from 1 is O(n + k). The binary search insight: at index i, the number of missing positives before arr[i] is exactly arr[i] − (i + 1), since without gaps arr[i] would equal i + 1. That count only grows along the array, so binary search finds where the k-th missing number falls.
Example: arr = [2, 3, 4, 7, 11], k = 5 → 9 (missing: 1, 5, 6, 8, 9, ...).
int findKthPositive(const vector<int>& arr, int k); // arr strictly increasing positive integers
Input (arr, k) | Output | Explanation |
|---|---|---|
([2, 3, 4, 7, 11], 5) | 9 | Missing 1, 5, 6, 8, 9 |
([1, 2, 3, 4], 2) | 6 | Missing 5, 6 |
([5, 6, 7], 4) | 4 | Missing 1, 2, 3, 4 before the array starts |
([1], 1) | 2 | |
([2], 1) | 1 |
(Confirm: arr is strictly increasing; the answer may be smaller than arr[0] or larger than the last element.)
arr[i] − (i + 1) the number of missing values before arr[i]?"lo + k?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in