Skip to interview prep
Apexed

12. Tower of Hanoi (list the moves)

EasyRecursion & backtracking

Move n disks from peg "A" to peg "C" using peg "B", never placing a larger disk on a smaller one. Return the moves as strings like "A->C", in order.

Example 1

Input: n = 2
Output: ["A->B", "A->C", "B->C"]

Constraints

  • 1 ≤ n ≤ 10

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 3 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Tower of Hanoi (list the moves) — coding interview practice — Apexed