Find the Duplicate Number (Floyd's Cycle)

Algorithms
Medium
LinkedIn
48.9K views

Given an array of $n+1$ integers where each integer is between 1 and $n$ (inclusive), find the single duplicate number using $O(1)$ extra space.

Why Interviewers Ask This

Interviewers at LinkedIn ask this to assess your ability to recognize non-obvious mathematical patterns in data structures. They specifically evaluate whether you can map array indices to a linked list cycle, demonstrating deep algorithmic thinking beyond standard sorting or hashing solutions. This tests your mastery of space-time trade-offs under strict O(1) memory constraints.

How to Answer This Question

Structure your response using the 'Pattern Recognition and Constraint Mapping' framework. First, explicitly acknowledge the constraints: O(1) space forbids hash sets, and O(n log n) time forbids sorting. Second, visualize the problem by treating the array as a functional graph where index i points to value nums[i], creating a directed cycle due to the duplicate number. Third, explain Floyd's Tortoise and Hare algorithm as two pointers moving at different speeds to detect this cycle. Fourth, detail the two-phase process: first finding the intersection point inside the cycle, then resetting one pointer to the start to find the entry point which is the duplicate. Finally, analyze the complexity to prove both time and space requirements are met, showing you understand why this specific approach is optimal for large-scale data systems like those at LinkedIn.

Key Points to Cover

  • Explicitly mapping array indices to a linked list structure to reveal the hidden cycle
  • Acknowledging why standard hashing or sorting violates the O(1) space constraint
  • Correctly distinguishing between the cycle detection phase and the entry point calculation phase
  • Proving the mathematical validity that the intersection of the second phase equals the duplicate value
  • Demonstrating awareness of LinkedIn's focus on scalable, memory-efficient algorithms

Sample Answer

To solve this efficiently within O(1) space, I treat the input array not just as a list, but as a linked list where each index points to the next node via its value. Since there are n+1 numbers between 1 and n, at least two indices must point to the same value, guaranteeing a cycle exists. The duplicate number is the entry point of this cycle. I would implement Floyd's Cycle Detection Algorithm. In phase one, I initialize two pointers, slow and fast, at the start. Slow moves one step while fast moves two steps until they meet. This meeting point confirms a cycle but isn't necessarily the duplicate. In phase two, I reset one pointer to the beginning of the array while keeping the other at the meeting point. Moving both one step at a time, their next intersection is the duplicate number. For example, with [1,3,4,2,2], index 0 points to 1, index 1 to 3, index 3 to 2, and index 2 to 4, which loops back to 2. The pointers will converge at index 2 (value 4) in phase one, then meet again at index 3 (value 2) in phase two. This approach runs in O(n) time and uses constant extra space, making it ideal for high-performance environments like LinkedIn's infrastructure where memory efficiency is critical.

Common Mistakes to Avoid

  • Attempting to use a HashSet or frequency array, which immediately fails the O(1) space requirement
  • Sorting the array first, which increases time complexity to O(n log n) and modifies the input
  • Stopping after detecting the cycle without performing the second phase to locate the actual duplicate value
  • Failing to explain the mathematical logic behind why the two pointers meet at the duplicate in the second phase

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 26 LinkedIn questions