Find the Duplicate Number Without Modifying the Array
Updated
Problem statement
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 harder versions are for discussion only. Your function solves the main problem above.
Function
findDuplicate(nums: int[]) → intExamples
Example 1
nums = [1,3,4,2,2]return = 2The value 2 appears twice, while every other value appears once.
Example 2
nums = [3,1,3,4,2]return = 3The duplicated value is 3.
Example 3
nums = [1,1]return = 1This 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.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEW
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEW
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEW
- Shortest Window with Required CountsOracle · ONSITE INTERVIEW
- 3Sum SmallerSalesforce · ONSITE INTERVIEW
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEW
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEW
- Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEW
- Rotate an Array Right by K StepsQualys · ONSITE INTERVIEW