Find the Duplicate Number
Reported by candidates from SpaceX's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
SpaceX reported this one in January 2021, and it's less scary than the name suggests. Strip the wrapper and it's a pigeonhole problem: n numbers squeezed into the range 1 to n - 1, so one value has to repeat. That means a hash set solves it in a minute, and the real question is whether you can do better. If you've got an OA invite and 48 hours, know the three approaches cold. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the working solution in real time.
The problem
Find the Duplicate Number You are given an integer array nums of length n. It contains every integer from 1 through n - 1 exactly once, except for one value that appears twice. Return the duplicated value. Function findDuplicateNumber(nums: int[]) → int Examples Example 1 nums = [1,3,4,2,2] return = 2 The value 2 is the only number that appears twice. Example 2 nums = [3,1,3,4,2] return = 3 The value 3 appears at two positions. Example 3 nums = [1,1] return = 1 With n = 2, the only valid value is 1, and it appears twice. Constraints 2 ≤ nums.length ≤ 100,000 Every value is between 1 and nums.length - 1, inclusive. Exactly one distinct value appears twice, and every other value appears once.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The easy path is a hash set. Walk the array, return the first value you've already seen. O(n) time, O(n) space. It's correct and it passes. The catch is that this problem is famous for the follow-up: solve it in O(1) extra space without modifying the array. Two ways do that. First, treat values as pointers, since every value is a valid index, so the array is a linked list with a cycle. Floyd's tortoise and hare finds the cycle entry, which is the duplicate. Second, binary search on the value range, counting how many elements are less than or equal to mid. The common pitfall is the second phase of Floyd's: after the pointers meet, reset one to index 0 and move both one step at a time. Mixing up that reset gives wrong answers on [1,1]. If the live OA freezes you, StealthCoder is the hedge that hands you the clean version.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Find the Duplicate Number cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find the duplicate number. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SpaceX's OA.
SpaceX reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the Duplicate Number FAQ
How hard is Find the Duplicate Number really?+
The hash set version is easy. The constrained version, constant space with no modifying the array, is medium and trips people up. Know the set solution first so you have a passing answer, then add Floyd's cycle detection if you have time to spare.
What's the trick to solving it without extra space?+
Treat each value as a pointer to an index. Since values are 1 to n - 1 and the array has n slots, following pointers from index 0 must enter a cycle. The duplicate value is the node where the cycle begins. Floyd's two-pointer method finds it.
Can I just sort the array?+
Yes. Sort, then scan for two adjacent equal values. It's O(n log n) time and works fine, but it modifies the array or costs a copy. It's a decent fallback if your memory of Floyd's is shaky, and it's easy to get right.
Does the binary search approach work here?+
Yes. Binary search on the value range 1 to n - 1. For a mid value, count elements less than or equal to mid. If the count exceeds mid, the duplicate is in the lower half. Otherwise it's higher. It's O(n log n) time and O(1) space.
How do I prepare in 48 hours for a SpaceX OA like this?+
Write the hash set, sorting, and Floyd's versions from memory once each. Test them on [1,1], [1,3,4,2,2], and [3,1,3,4,2]. Then practice explaining why a cycle must exist. That covers this problem and its common follow-ups.