DSA interview question
A ticketing system issues seat numbers 0 to n for an event of n + 1 seats, and after the doors close it has a list of the n seats that were scanned, all distinct. Exactly one seat number is missing from the list, and security wants to know which one. Sorting the list or building a lookup set would work, but there's a neat O(n)-time, O(1)-space answer: the numbers 0..n have a known total (or a known XOR), so whatever is missing from the list's total is the missing seat.
Example: [3, 0, 1] (n = 3, range 0..3) → 2.
int missingNumber(const vector<int>& nums); // nums has n distinct values from [0, n]
Input nums | Output | Explanation |
|---|---|---|
[3, 0, 1] | 2 | Range 0..3 |
[0, 1] | 2 | The missing number can be n itself |
[9, 6, 4, 2, 3, 5, 7, 0, 1] | 8 | |
[0] | 1 | |
[1] | 0 | The missing number can be 0 |
(Confirm: all values are distinct and lie in [0, n], where n is the array's length; exactly one number is missing.)
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