WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Coding Interview Essentials Β· Recursion and Dynamic Programming Problems Β· question 72 of 120

How would you solve the Tower of Hanoi problem using recursion?

πŸ“• Buy this interview preparation book: 120 Coding Interview Essentials questions & answers β€” PDF + EPUB for $5

The Tower of Hanoi problem is a mathematical puzzle. It consists of three poles and a number of disks of different sizes, which can slide onto any pole. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

1. Only one disk can be moved at a time.

2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.

3. No disk may be placed on top of a smaller disk.

We can solve the problem using a recursive approach. The key to the solution is to break the problem down into smaller subproblems.

We can write the recursive function to solve the problem as follows:

1. Move the top β€˜nβˆ’1β€˜ disks from pole β€˜Aβ€˜ (source pole) to pole β€˜Bβ€˜ (auxiliary pole), using pole β€˜Cβ€˜ (destination pole).

2. Move the remaining disk from pole β€˜Aβ€˜ to pole β€˜Cβ€˜.

3. Move the β€˜nβˆ’1β€˜ disks from pole β€˜Bβ€˜ to pole β€˜Cβ€˜, using pole β€˜Aβ€˜.

Let’s denote this function as β€˜Hanoi(n, A, B, C)β€˜. The pseudocode for the function is as follows:

function Hanoi(n, source, auxiliary, target) {
    if (n > 0) {
        // Move n-1 disks from source to auxiliary, using target as temporary pole
        Hanoi(n-1, source, target, auxiliary);
        
        // Move the nth disk from source to target
        print "Move disk " + n + " from pole " + source + " to pole " + target;
        
        // Move the n-1 disks that we left on auxiliary to target
        Hanoi(n-1, auxiliary, source, target);
    }
}

Then, to solve the problem with β€˜nβ€˜ disks, you would call the function as β€˜Hanoi(n, ’A’, ’B’, ’C’)β€˜.

The time complexity of the algorithm is given by the recurrence relation β€˜T(n) = 2T(n-1) + O(1)β€˜. First, we ignore the minor operations β€˜O(1)β€˜. The solution to this recurrence relation results in T(n) = 2nβ€…βˆ’β€…1 operations. Therefore, the time complexity of the Tower of Hanoi problem is O(2n).

This is because for each disk β€˜nβ€˜, we execute a recursive function twice to solve the subproblem of size β€˜n-1β€˜. Hence, we have β€˜2T(n-1)β€˜. The β€˜+ O(1)β€˜ takes care of the moving operation.

The space complexity of the algorithm is β€˜O(n)β€˜. This stems from the maximum depth of recursion we hit when using an explicit stack to keep track of function calls (recursion), which in the worst case is β€˜nβ€˜ (where β€˜nβ€˜ is the number of disks). Each function call requires a constant amount of space for housekeeping tasks and hence we get linear space complexity.

Let’s solve the problem for 3 disks:

Step 1: Call `Hanoi(3, 'A', 'B', 'C')`

- Call `Hanoi(2, 'A', 'C', 'B')`

    - Call `Hanoi(1, 'A', 'B', 'C')`
        - Call `Hanoi(0, 'A', 'C', 'B')` - Base case, do nothing
        - Move disk 1 from pole A to pole C
        - Call `Hanoi(0, 'B', 'A', 'C')` - Base case, do nothing
    - Move disk 2 from pole A to pole B
    - Call `Hanoi(1, 'C', 'A', 'B')`
        - Call `Hanoi(0, 'C', 'B', 'A')` - Base case, do nothing
        - Move disk 1 from pole C to pole B
        - Call `Hanoi(0, 'A', 'C', 'B')` - Base case, do nothing

- Move disk 3 from pole A to pole C

- Call `Hanoi(2, 'B', 'A', 'C')`
    - Call `Hanoi(1, 'B', 'C', 'A')`
        - Call `Hanoi(0, 'B', 'A', 'C')` - Base case, do nothing
        - Move disk 1 from pole B to pole A
        - Call `Hanoi(0, 'C', 'B', 'A')` - Base case, do nothing
    - Move disk 2 from pole B to pole C
    - Call `Hanoi(1, 'A', 'B', 'C')`
        - Call `Hanoi(0, 'A', 'C', 'B')` - Base case, do nothing
        - Move disk 1 from pole A to pole C
        - Call `Hanoi(0, 'B', 'A', 'C')` - Base case, do nothing

This results in the sequence of moves:

- Move disk 1 from pole A to pole C

- Move disk 2 from pole A to pole B

- Move disk 1 from pole C to pole B

- Move disk 3 from pole A to pole C

- Move disk 1 from pole B to pole A

- Move disk 2 from pole B to pole C

- Move disk 1 from pole A to pole C

So, this is the list of moves to solve the Tower of Hanoi problem for 3 disks.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Coding Interview Essentials interview β€” then scores it.
πŸ“ž Practice Coding Interview Essentials β€” free 15 min
πŸ“• Buy this interview preparation book: 120 Coding Interview Essentials questions & answers β€” PDF + EPUB for $5

All 120 Coding Interview Essentials questions Β· All topics