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 · Stack and Queue Problems · question 57 of 120

Can you solve the Tower of Hanoi problem using stacks?

📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

The Tower of Hanoi puzzle is a classical problem of recursion and can indeed be solved using stacks in the context of computer programming.

The Tower of Hanoi problem is a puzzle that involves moving n discs of different sizes from one rod to another, using a certain set of rules:

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

2. A disk can only be moved if it is the uppermost disk on a stack.

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

To solve it using stacks, you can exploit the simple fact that a stack of disks in descending order (largest disk at the bottom, smallest at the top) represents a valid state of a Hanoi rod.

Assuming you have three rods (or three stacks), namely source, auxiliary, and destination, here’s how you would solve it:

1. Move n-1 disks from the source to the auxiliary rod (using the destination as the temporary rod).

2. Move the nth disk from the source to the destination rod.

3. Move the n-1 disks from the auxiliary rod to the destination rod (using the source rod as the temporary rod).

Given this, a possible Python solution using stacks could look like this:

def hanoi(n, source, destination, auxiliary):
    if n > 0:
        # Move n - 1 disks from source to auxiliary, so they are out of the way
        hanoi(n - 1, source, auxiliary, destination)

        # Move the nth disk from source to destination
        destination.append(source.pop())

        # Move the n - 1 disks that we left on auxiliary to destination
        hanoi(n - 1, auxiliary, destination, source)

# Initiate call from source rod to destination rod with auxiliary rod      
source = [3, 2, 1]
destination = []
auxiliary = []
hanoi(len(source), source, destination, auxiliary)

In this example, "source", "destination" and "auxiliary" are all stacks that simulate the rods in the problem. Initially, all disks are on the "source" rod. After calling hanoi, all disks end up on the "destination" rid, obeying the rules of the puzzle.

Just remember that this is a recursive function and it solves this problem in O(2n) time complexity, where n is the number of disks. This isn’t the most efficient solution in terms of time complexity, but the Tower of Hanoi problem is a classic example of a problem where the solution naturally fits a recursive algorithmic approach based on the problem’s rules.

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