Find All Duplicates
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.
Find all values that appear twice; return sorted. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A data load can contain ids that were sent twice. You need the ids that occur exactly twice, not every id that appears once. Find all values that appear twice in the list and return them in ascending order. Each value in the input appears once or twice, so a value is reported when its second occurrence is found. Write the function find_duplicates(nums: list) -> list. In [4, 3, 2, 7, 8, 2, 3, 1], the repeated values are 2 and 3, so the answer is [2, 3]. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.
Requirements
- Return every value that occurs twice.
- Return each repeated value once.
- Sort the result from smallest to largest.
Constraints
- The list has length n and every value is in 1 to n.
- Each value appears once or twice, and the return type is list.
Examples
Input: find_duplicates([4, 3, 2, 7, 8, 2, 3, 1]) Output: [2, 3] 2 and 3 appear twice.
Topics: lakebench, python, duplicates, range.
More Python interview questions · All interview problems · Learn data engineering
Find All Duplicates
Interview-style drill: Find all values that appear twice; return sorted.
Implement `find_duplicates(nums: list) -> list` for nums where each value is in `1..n` and appears once or twice. Return sorted list of values that appear twice. Example: `[4, 3, 2, 7, 8, 2, 3, 1]` → `[2, 3]`. Keep the harness.