Find Duplicate Number
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.
Find the one duplicated number in 1..n with n+1 entries. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
An id stream contains n plus 1 values, but the allowed ids are only 1 through n. One id must therefore have been sent twice. Find and return the one duplicated number. The input can be in any order, and you must return the value itself, not its positions or its count. Write the function find_duplicate(nums: list) -> int. In [1, 3, 4, 2, 2], the second 2 is the repeated id, so the answer is 2. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.
Requirements
- Return the only value that appears more than once.
- Work without assuming the input is sorted.
- Return an integer value and do not return a list.
Constraints
- There are n + 1 integers, each in 1 to n.
- Exactly one value is duplicated, and the return type is int.
Examples
Input: find_duplicate([1, 3, 4, 2, 2]) Output: 2 2 appears twice.
Topics: lakebench, python, duplicate, floyd.
More Python interview questions · All interview problems · Learn data engineering
Find Duplicate Number
Interview-style drill: Find the one duplicated number in 1..n with n+1 entries.
Implement `find_duplicate(nums: list) -> int` where nums has n+1 integers in 1..n and exactly one duplicate. Example: `[1, 3, 4, 2, 2]` → `2`. Keep the harness.