Back to Data Structures and Algorithms Interview
Data Structures and Algorithms Interview

Data Structures and Algorithms Interview

0 of 20 Completed

Revisiting A Tower of Hanoi

The Tower of Hanoi is a classic logic problem that will require a bit of trial and error. If you solved the problem and wrote out your steps, congrats: What you created is an algorithm!

But, as you’ve seen earlier, algorithms can have varying degrees of efficiency. Reaching the correct solution isn’t the only important part as there could be more and less efficient routes to that solution.

Here is the most efficient algorithm for a three-rod, three-disk Tower of Hanoi:

  1. Move Disc 1 (Smallest) to the Rightmost Rod: First, move the smallest disc (Disc 1) from the leftmost rod to the rightmost rod.
  2. Move Disc 2 (Medium) to the Middle Rod: Next, move the medium disc (Disc 2) from the leftmost rod to the middle rod.
  3. Move Disc 1 to the Middle Rod, on top of Disc 2: Then, move Disc 1 from the rightmost rod to the middle rod, placing it on top of Disc 2.
  4. Move Disc 3 (Largest) to the Rightmost Rod: Now, move the largest disc (Disc 3) from the leftmost rod to the rightmost rod.
  5. Move Disc 1 to the Leftmost Rod: Move Disc 1 from the middle rod back to the leftmost rod.
  6. Move Disc 2 to the Rightmost Rod, atop Disc 3: Move Disc 2 from the middle rod to the rightmost rod, placing it on top of Disc 3.
  7. Move Disc 1 to the Rightmost Rod, completing the puzzle: Finally, move Disc 1 from the leftmost rod to the rightmost rod, placing it on top of the stack.

To extend this solution, can you create a general algorithm for n-rods or n-disks?

0%

Completed

You have 20 sections remaining on this learning path.