Find Missing Number
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.
Find the missing number from the range 0..n given n numbers. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
You have a list of numbers that should contain every whole number from 0 up to n, but exactly one of them is missing. Your job is to find the missing one. Write a function missing_number(nums). The list nums has n numbers, and they all come from the range 0 to n (including both ends). All numbers are different, and exactly one number from that range is not in the list. Return that missing number. For [3, 0, 1] there are 3 numbers, so the range is 0, 1, 2, 3. The numbers 0, 1 and 3 are present, so the missing one is 2. Keep the print lines in the editor.
Requirements
- Return the one number from 0 to n that is not in the list.
- The list is not sorted, so do not assume any order.
- Return a single integer.
Constraints
- nums has length n, and every value is in the range 0 to n.
- All values are different, and exactly one number from the range is missing.
- The range has n + 1 numbers but the list only has n, which is why one is missing.
Examples
Input: missing_number([3, 0, 1]) Output: 2 0..3 missing 2.
Topics: lakebench, python, math, xor.
More Python interview questions · All interview problems · Learn data engineering
Find Missing Number
Interview-style drill: Find the missing number from the range 0..n given n numbers.
Implement `missing_number(nums: list) -> int` where nums contains n distinct numbers from `0..n` with one missing. Example: `[3, 0, 1]` → `2`. Keep the harness.