Dsa
Recursion
Medium
Tower of Hanoi
Implement the Tower of Hanoi problem. You have three pegs and n disks of different sizes which can slide onto any peg. The puzzle starts with the disks stacked in ascending order of size on one peg, the smallest at the top. You need to move the entire stack to another peg, obeying the following rules: only one disk can be moved at a time, no larger disk can be placed on top of a smaller disk. Your output should show the sequence of moves as 'from_peg -> to_peg'. For n=2, the output should be:
1 -> 3
1 -> 2
3 -> 2
Example Input:
2
Example Output:
1 -> 3
1 -> 2
3 -> 2
Key concepts
recursionstacksalgorithms
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now