00:00

Problem Statement

Input/Output Examples

Visual Explanation

Each level makes two recursive calls on n - 1 disks plus one move, so moves(n) = 2 × moves(n - 1) + 1, which solves to 2n − 1.

Approaches

Both approaches print exactly the same moves. One recurses; the other computes each move from its number.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of disks. Both approaches produce all 2n − 1 moves, which is also the proven minimum.

Interview Tips and Recommended Approach

  • Explain the three steps before writing code; the recursion follows immediately.
  • Derive the move count from the recurrence T(n) = 2T(n - 1) + 1.
  • Know the pattern. Disk 1 moves every other turn, always cycling through the pegs in the same direction.
  • Warn about growth. 64 disks would need about 1.8 × 1019 moves.

Recommended Approach: Recursion

Why: it states the solution exactly as you would explain it, and it is the clearest demonstration of how recursion breaks a problem into smaller copies of itself.