Asked at 12 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- Yahoo
- Netflix
- Capgemini
- Airbnb
- tcs
+2 moreShow fewer
- ZScaler
- SAP
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Counting subarrays with a map of totals
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.
ProblemProblem statementGiven an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.ReadHide
Problem Statement
Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.
Re-solved 2026-10-09 as Day 1 of the foundations rebuild.
Input / Output
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Understanding the Problem
- Same "have I seen this before?" instinct as Two Sum — but here we only need membership, not indices, so a set is enough
set()(not{}— that's a dict) gives O(1) average membership checks- Early return on the first repeat;
Falseonly after the full scan
Edge Cases
- Empty or single-element array →
False, loop simply never triggers - Duplicate at the very end → still caught, just later
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Hash Set Membership — the lighter sibling of the hash-map pattern: when you need "seen before?" without positions, a set is all you need.
Approach
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Sort | O(n log n) | O(1)/O(n) | Sort then scan neighbours |
| Hash set | O(n) | O(n) | One pass, early exit |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n^2) time | O(1) space — for each element, scan the rest."""
def containsDuplicate(self, nums: List[int]) -> bool:
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
return True
return False
class Solution:
"""O(n) time | O(n) space — one pass with a set, early exit."""
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return FalseOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language