How to Explain Contains Duplicate in a Coding Interview (Step-by-Step)
Contains Duplicate is one of the first problems interviewers use to warm you up — and a surprisingly easy one to over-complicate. Most candidates jump straight to sorting or nested loops when a single HashSet is all you need.
In this post, you’ll learn exactly how to walk an interviewer through this problem: what to say, in what order, and which tradeoffs to proactively mention so you come across as sharp and experienced.
Problem Statement
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Examples
Output: true
Output: false
Output: true
Constraints
1 <= nums.length <= 10⁵-10⁹ <= nums[i] <= 10⁹
What Is the Problem?
Before writing a single line of code, restate the problem in your own words:
“I need to check whether any number appears more than once in the array. If yes, returntrue. If all numbers are unique, returnfalse.”
Simple restatement — but it confirms to the interviewer that you’ve read the problem correctly before diving in.
Step 1: Start With the Brute Force (Then Dismiss It)
Always acknowledge the naive approach first:
“The brute force is two nested loops — for every element, check if any other element equals it. That’s O(n²) time which is too slow for an array of 100,000 elements.”
Then mention the sorting approach as a middle ground before landing on optimal:
“I could also sort the array first — duplicates would be adjacent, so one pass would find them. That’s O(n log n) time but it modifies the input. The cleanest solution is a HashSet: O(n) time, O(n) space, no mutation.”
Two nested loops
Time: O(n²) Space: O(1)
Sort then scan adjacents
Time: O(n log n) Space: O(1)
Single pass, track seen values in a set
Time: O(n) Space: O(n)
Step 2: Explain the Algorithm in Plain English
Before touching code, describe the approach out loud:
“I iterate through the array once. For each number, I check if it’s already in my HashSet. If it is — I’ve found a duplicate, returntrue. If not, I add it to the set and move on. If I finish the loop without finding a duplicate, I returnfalse.”
Step 3: Talk Through the Code
Here’s the Python solution with inline comments:
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set() # HashSet to track numbers we've already visited
for num in nums:
if num in seen: # Duplicate found — no need to continue
return True
seen.add(num) # First time seeing this number — record it
return False # Completed the loop with no duplicates found
Point out the early return — as soon as we find one duplicate we stop. No need to scan the rest of the array.
Step 4: Highlight Edge Cases
“Ifnumshas only one element, the loop runs once, nothing is inseenyet, we add it and returnfalse. Correct.”
“If every element is the same, e.g.[3, 3, 3], we returntrueon the second iteration. Early exit keeps it fast.”
“The constraints allow negative numbers — but a Python set handles any hashable value, so negatives work exactly the same as positives. No special handling needed.”
Step 5: State Time and Space Complexity
| Dimension | Complexity | Reason |
|---|---|---|
| Time | O(n) | Single pass — each element checked and inserted once |
| Space | O(n) | Set grows up to n elements in the worst case (all unique) |
Step 6: Mention the Space vs Time Tradeoff
Proactively raising tradeoffs shows senior-level thinking:
“There’s a classic tradeoff here. The sorting approach uses O(1) extra space but takes O(n log n) time and mutates the input. The HashSet approach is O(n) time but costs O(n) space. If the interviewer says memory is constrained, I’d go with sorting — otherwise HashSet is the better choice.”
Most interviewers will nod here — it’s exactly the kind of thinking they want to see.
Interview Checklist for Contains Duplicate
- Restate — “check if any number appears more than once”
- Mention brute force O(n²), then sorting O(n log n), then HashSet O(n)
- Explain the single-pass HashSet approach in plain English
- Point out the early return optimization
- Handle single element, all duplicates, and negative number edge cases
- State O(n) time and O(n) space
- Raise the space vs time tradeoff proactively
- Ask before coding, don’t just dive in
Related Problems to Practice
If you can solve Contains Duplicate cleanly, try these next:
