The Find the Duplicate Number interview question is a classic algorithmic challenge. You are given an array of integers where each integer is in the range . According to the Pigeonhole Principle, at least one duplicate number must exist. Your task is to find the duplicate number without modifying the array and using only extra space.
This Find the Duplicate Number coding problem is a favorite of top tech firms like Google, Microsoft, and Cisco. It tests your ability to look at a problem from multiple perspectives: as a search problem (Binary Search), a bit manipulation problem, or most elegantly, as a graph problem. It evaluates whether you can find a solution that satisfies strict constraints on space and mutation.
The most optimal solution uses Floyd's Cycle-Finding Algorithm (Tortoise and Hare).
nums[i] as a directed edge to node nums[i]. Because there's a duplicate, the "path" will eventually form a cycle.slow by one step and fast by two steps. They will eventually meet inside the cycle.slow back to the start and move both at the same speed. They will meet at the entrance of the cycle, which corresponds to the duplicate number.Array: [1, 3, 4, 2, 2]
nums[0] = 1.0 -> 1 -> 3 -> 2 -> 2 -> 2...Master the "Graph Linked List" analogy. Any array where the values are within the range of indices can be treated as a functional graph. This is a powerful Two Pointers interview pattern that solves many "Cycle" related questions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Two Sum II - Input Array Is Sorted | Medium | Solve | |
| Maximum Distance Between a Pair of Values | Medium | Solve | |
| Count the Number of Incremovable Subarrays II | Hard | Solve | |
| Find Products of Elements of Big Array | Hard | Solve | |
| Heaters | Medium | Solve |