By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 FAANG onsite interviews—master it, and you instantly prove you can optimize for time, space, and edge cases under pressure."
Before tackling "Contains Duplicate," ensure you understand: 1. Hash Sets – O(1) average-time insert/lookup, used for tracking seen elements. 2. Time-Space Tradeoffs – When to use extra space (hash set) vs. sorting (O(1) space but O(n log n) time). 3. Edge Cases – Empty input, single-element input, all duplicates, no duplicates.
if num in seen: return True
nums.sort(); for i in range(1, n): if nums[i] == nums[i-1]: return True
for num in nums: if num in seen: return True; seen.add(num)
for i in range(n): if nums[i] in seen and i - seen[nums[i]] <= k: return True
from collections import Counter; return any(v >= 2 for v in Counter(nums).values())
Run this every time you see a "Contains Duplicate" variant:
k
Are we allowed to modify the input (e.g., sorting)?
Choose the Right Technique
Follow-up (e.g., "within k range")? → Hash map (track indices).
Handle Edge Cases
False
All duplicates → Return True immediately.
True
Optimize for Early Termination
Exit as soon as a duplicate is found (don’t process the entire array).
Write Pseudocode First
Sketch the logic before coding (e.g., for num in nums: if num in seen: return True).
for num in nums: if num in seen: return True
Code & Test
Write the solution, then test with:
[1, 2, 3, 1]
[1, 2, 3, 4]
[]
[1, 1, 1, 1]
Analyze Time & Space Complexity
Problem: Given an integer array nums, return True if any value appears at least twice, else False.
nums
seen = set() for num in nums: if num in seen: return True seen.add(num) return False
python def containsDuplicate(nums): seen = set() for num in nums: if num in seen: return True seen.add(num) return False
Why This Works: - A hash set provides O(1) lookups, so we can check for duplicates in a single pass.
Problem: Given an integer array nums and an integer k, return True if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.
i
j
nums[i] == nums[j]
abs(i - j) <= k
k = 0
nums[i] == nums[i]
seen = {} for i, num in enumerate(nums): if num in seen and i - seen[num] <= k: return True seen[num] = i return False
python def containsNearbyDuplicate(nums, k): seen = {} for i, num in enumerate(nums): if num in seen and i - seen[num] <= k: return True seen[num] = i return False
Why This Works: - By tracking the last index of each number, we can check if the current index is within k of the previous occurrence.
Problem: Given an integer array nums and two integers k and t, return True if there are two distinct indices i and j such that abs(nums[i] - nums[j]) <= t and abs(i - j) <= k.
t
abs(nums[i] - nums[j]) <= t
<= t
t = 0
bucket = {} for i, num in enumerate(nums): bucket_id = num // (t + 1) if bucket_id in bucket: return True if bucket_id - 1 in bucket and abs(num - bucket[bucket_id - 1]) <= t: return True if bucket_id + 1 in bucket and abs(num - bucket[bucket_id + 1]) <= t: return True bucket[bucket_id] = num if i >= k: del bucket[nums[i - k] // (t + 1)] return False
python def containsNearbyAlmostDuplicate(nums, k, t): if t < 0 or k < 0: return False bucket = {} for i, num in enumerate(nums): bucket_id = num // (t + 1) if bucket_id in bucket: return True if bucket_id - 1 in bucket and abs(num - bucket[bucket_id - 1]) <= t: return True if bucket_id + 1 in bucket and abs(num - bucket[bucket_id + 1]) <= t: return True bucket[bucket_id] = num if i >= k: del bucket[nums[i - k] // (t + 1)] return False
Why This Works: - Bucketing groups numbers into ranges of size t + 1, allowing us to check adjacent buckets for values within t. - Sliding window ensures we only consider indices within k distance.
t + 1
if not nums: return False
abs(nums[i] - nums[j])
abs(num - bucket_val) <= t
n
min(k, n)
"Alright, let’s lock this in. When you see ‘Contains Duplicate,’ ask yourself: 1. Are duplicates anywhere in the array? → Hash set, O(n) time. 2. Are duplicates within k indices? → Hash map tracking last seen index. 3. Are we checking close values (<= t) within k indices? → Bucketing + sliding window.
Always handle edge cases: empty array, single element, all duplicates. Optimize for early termination—exit as soon as you find a match. If they ask for O(1) space, sort first and check adjacent elements. And if they throw a curveball like ‘What if k is larger than n?’, just cap the window at the array size.
You’ve got this. Now go crush that interview." ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.