#287

Find the Duplicate Number

MediumFloydHas vizArrayTwo PointersBinary Search

Problem

Find the Duplicate Number

You must solve the interview problem titled "Find the Duplicate Number".

Difficulty: Medium. Topics: Array, Two Pointers, Binary Search. Primary pattern: Floyd.

Read the title as the specification: implement the exact behavior it names, return the correct output type, and aim for an interview-optimal Floyd solution. Use the examples to lock input/output format, then confirm edge cases (empty, single element, duplicates, extremes).

Example 1

Input: sample input for Find the Duplicate Number

Output: sample output for Find the Duplicate Number

Apply a Floyd approach and match the required return type.

Example 2

Input: edge-case input for Find the Duplicate Number

Output: edge-case output for Find the Duplicate Number

Verify empty/single/boundary behavior.

Constraints

  • Interview-scale limits for a Medium Floyd problem.
  • Handle empty and single-element inputs when they are valid.
  • Prefer the optimal asymptotic complexity expected in interviews.
Loading visualization…

Explanation

Find the Duplicate Number is a Medium interview problem tagged [Array, Two Pointers, Binary Search]. The winning approach is typically **Floyd**.

Intuition

Duplicate-number problems map to cycle detection without modifying the array.

Approach

Treat indices as a functional graph; use fast/slow to find cycle, then find entrance.

  1. Advance slow by 1, fast by 2 via nums[i] jumps.
  2. Meet inside cycle.
  3. Reset one to start; advance both by 1 to entrance.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateFloyd

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) or O(n)

Space: O(n)

Before coding, restate the problem, name the pattern (Floyd), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #287 Find the Duplicate Number as understood when the pattern clicks.