Find the Duplicate Number Without Modifying the Array
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google reported this one in September 2026, and the constraints are the whole story. n goes up to 100000, the array can't be modified, and extra space has to be O(1). That kills the nested loop, the hash set, and the sort-in-place trick in one shot. You're left with two real options: binary search on the value range, or treating the array as a linked list and running cycle detection. If you've got an OA invite and you blank on the cycle trick, StealthCoder is the safety net running invisibly during the live assessment.
The problem
You are given an integer array nums of length n + 1. Every value is between 1 and n, inclusive, and exactly one distinct value appears more than once. Return the duplicated value. The duplicated value may occur more than twice. Do not modify nums. Use O(1) auxiliary space and strictly better than O(n^2) time. Interview follow-up Be prepared to explain which guarantees the constant-space algorithm relies on and how the available approaches change if several distinct values may repeat or if values are not restricted to 1 through n. These generalized variants are discussion-only; the judged function uses the primary contract above. Function findDuplicate(nums: int[]) → int Examples Example 1 nums = [1,3,4,2,2] return = 2 The value 2 appears twice, while every other value appears once. Example 2 nums = [3,1,3,4,2] return = 3 The duplicated value is 3. Example 3 nums = [1,1] return = 1 This is the smallest valid array, and 1 is duplicated. Constraints 1 <= n <= 100000. nums.length == n + 1. 1 <= nums[i] <= n. Exactly one distinct value is duplicated, possibly more than twice. The input array must not be modified.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Values sit in 1..n and indexes run 0..n, so treat nums[i] as a pointer from i to nums[i]. Index 0 is never pointed to, so you enter the structure from outside, and the duplicate is where two pointers land on the same node. That's the cycle entrance. Run Floyd's tortoise and hare: move slow by one, fast by two until they meet, then reset one pointer to the start and move both by one. They meet at the duplicate. O(n) time, O(1) space. The fallback is binary search on the value: count how many elements are <= mid, and if that count exceeds mid, the duplicate is in the lower half. That's O(n log n) and easier to get right. The common pitfall is sorting or marking visited entries, which breaks the no-modify rule. If the cycle logic slips mid-OA, StealthCoder is your hedge for the live assessment.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find the Duplicate Number Without Modifying the Array 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 would have shipped this the night before his JPMorgan OA if he'd had it.
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 Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the Duplicate Number Without Modifying the Array FAQ
What's the trick for Google's Find the Duplicate Number?+
Treat the array as a linked list where index i points to nums[i]. Because values are 1..n in an array of n+1 slots, a cycle must exist, and its entrance is the duplicate. Use Floyd's two-phase cycle detection to find that entrance in O(n) time and O(1) space.
Can I just use a hash set or sort the array?+
Not under this contract. A hash set uses O(n) space, and sorting modifies the array unless you copy it, which is also O(n) space. The problem explicitly forbids both, so you need cycle detection or binary search on the value range.
Is binary search on the value range acceptable?+
Yes. It beats O(n^2) and uses O(1) space. Pick mid in 1..n, count elements <= mid, and if the count is greater than mid the duplicate is at or below mid. It runs in O(n log n), which is slower than Floyd's but simpler and harder to botch.
What follow-ups should I prepare for?+
Be ready to explain what the constant-space method depends on: values restricted to 1..n and exactly one repeated value. If several values repeat or the range is unrestricted, Floyd's guarantee breaks. You'd fall back to a hash set, sorting a copy, or a bit-level approach, trading space for generality.
How do I prepare in 48 hours?+
Code Floyd's algorithm from memory twice, including the second phase where you reset a pointer to the start. Then write the binary-search version as a backup. Test on [1,1], [3,1,3,4,2], and a case where the duplicate appears three or more times.