Contains Duplicate
Python data engineering interview problem. Difficulty: beginner. Pattern: Hashing. About 10 minutes. Free to practice.
True if any value appears more than once. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Before you load data you often want to check whether any value appears more than once, for example when ids should be unique. Write a function contains_duplicate(nums) that returns True if any value appears at least twice in the list, and False if every value is different. For [1, 2, 3, 1] the value 1 appears twice, so the answer is True. For [1, 2, 3, 4] the answer is False. Keep the print lines in the editor.
Requirements
- Return True as soon as any value shows up a second time.
- Return False when all values are different.
- Return False for an empty list.
Constraints
- The values can be anything that can be compared and hashed, such as numbers or strings.
- The list may be large, so avoid comparing every value with every other value.
Examples
Input: contains_duplicate([1, 2, 3, 1]) Output: True 1 appears twice.
Topics: lakebench, python, set, duplicate.
More Python interview questions · All interview problems · Learn data engineering
Contains Duplicate
Interview-style drill: True if any value appears more than once.
Implement `contains_duplicate(nums: list) -> bool`. Example: `[1, 2, 3, 1]` → `True`. Keep the harness.