As reported: ServiceNow Associate Machine Learning Engineer loop, round 1 (technical, 60 minutes), Oct 2025: find the repeating number in an array, with a follow-up to find a missing number; then linked-list cycle detection and a graph problem.
nums has n + 1 integers, each in [1, n]. Exactly one value is repeated (possibly more than twice). Return it without modifying nums and using only O(1) extra space.
This is LeetCode 287 — Find the Duplicate Number.
Example
[1,3,4,2,2] -> 2
[3,1,3,4,2] -> 3
[3,3,3,3,3] -> 3
Constraints: 1 ≤ n ≤ 10^5; nums.length = n + 1; 1 ≤ nums[i] ≤ n.
Code runner format: one line with nums. Implement findDuplicate(nums); the harness prints the value.
Follow-ups
- Find the missing number instead: an array of
ndistinct numbers from0…nmisses exactly one (LeetCode 268 — Missing Number).