PPractick

DSA interview question

Find the Missing Number

Easy · Algorithms · Topics: array, math, bit manipulation

The problem

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.

Function signature

int missingNumber(const vector<int>& nums);  // nums has n distinct values from [0, n]

Test cases

Input numsOutputExplanation
[3, 0, 1]2Range 0..3
[0, 1]2The missing number can be n itself
[9, 6, 4, 2, 3, 5, 7, 0, 1]8
[0]1
[1]0The 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.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the range 0..n or 1..n?
  2. Are the values guaranteed distinct, with exactly one missing?
  3. Can I modify the array?
  4. How large is n? (Sum overflow.)

Follow-ups to expect

  1. "Why does the sum formula work?"
  2. "Can the sum overflow?"
  3. "How does the XOR version work?"
  4. "Other approaches?"
  5. "Complexity?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in