Bird
Raised Fist0
DSA Goprogramming~20 mins

Tower of Hanoi Problem in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tower of Hanoi Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Tower of Hanoi moves for 2 disks
What is the output of the following Tower of Hanoi function call for 2 disks?
DSA Go
package main
import "fmt"

func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 1 {
        fmt.Printf("Move disk 1 from %s to %s\n", from, to)
        return
    }
    towerOfHanoi(n-1, from, aux, to)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}

func main() {
    towerOfHanoi(2, "A", "C", "B")
}
A
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
B
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
C
Move disk 2 from A to B
Move disk 1 from A to C
Move disk 2 from B to C
D
Move disk 1 from B to C
Move disk 2 from A to C
Move disk 1 from A to B
Attempts:
2 left
💡 Hint
Remember the recursive steps: move n-1 disks to auxiliary, move largest disk, then move n-1 disks to target.
🧠 Conceptual
intermediate
1:00remaining
Minimum moves required for Tower of Hanoi with 4 disks
What is the minimum number of moves required to solve the Tower of Hanoi problem with 4 disks?
A15
B8
C16
D31
Attempts:
2 left
💡 Hint
The minimum moves follow the formula 2^n - 1, where n is the number of disks.
🔧 Debug
advanced
1:30remaining
Identify the error in this Tower of Hanoi code snippet
What error will this Go code produce when run?
DSA Go
package main
import "fmt"

func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 0 {
        fmt.Printf("Move disk 0 from %s to %s\n", from, to)
        return
    }
    towerOfHanoi(n-1, from, aux, to)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}

func main() {
    towerOfHanoi(3, "A", "C", "B")
}
ACompilation error: missing return statement
BStack overflow due to infinite recursion
CPrints moves including disk 0, which is invalid
DRuns correctly with no errors
Attempts:
2 left
💡 Hint
Check the base case condition and what happens when n=0.
🚀 Application
advanced
1:30remaining
Number of moves for 5 disks after 10 moves
If you have solved 10 moves in the Tower of Hanoi problem with 5 disks, how many moves remain to complete the puzzle?
A21
B22
C23
D24
Attempts:
2 left
💡 Hint
Total moves for 5 disks is 2^5 - 1. Subtract moves done.
❓ Predict Output
expert
2:30remaining
Output of Tower of Hanoi moves for 3 disks with custom peg names
What is the output of the Tower of Hanoi function call for 3 disks with pegs named X, Y, Z?
DSA Go
package main
import "fmt"

func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 1 {
        fmt.Printf("Move disk 1 from %s to %s\n", from, to)
        return
    }
    towerOfHanoi(n-1, from, aux, to)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}

func main() {
    towerOfHanoi(3, "X", "Z", "Y")
}
A
Move disk 1 from X to Y
Move disk 2 from X to Y
Move disk 1 from Y to Z
Move disk 3 from X to Z
Move disk 1 from Y to X
Move disk 2 from Y to Z
Move disk 1 from X to Z
B
Move disk 1 from X to Y
Move disk 2 from X to Z
Move disk 1 from Y to Z
Move disk 3 from X to Z
Move disk 1 from Y to X
Move disk 2 from Z to Y
Move disk 1 from X to Y
C
Move disk 1 from X to Y
Move disk 2 from X to Z
Move disk 1 from Y to Z
Move disk 3 from X to Y
Move disk 1 from Z to X
Move disk 2 from Z to Y
Move disk 1 from X to Y
D
Move disk 1 from X to Z
Move disk 2 from X to Y
Move disk 1 from Z to Y
Move disk 3 from X to Z
Move disk 1 from Y to X
Move disk 2 from Y to Z
Move disk 1 from X to Z
Attempts:
2 left
💡 Hint
Follow the recursive pattern carefully with the given peg names.

Practice

(1/5)
1. What is the minimum number of moves required to solve the Tower of Hanoi problem with 3 disks?
easy
A. 6
B. 9
C. 8
D. 7

Solution

  1. Step 1: Understand the formula for minimum moves

    The minimum moves required for n disks is 2^n - 1.
  2. Step 2: Calculate for 3 disks

    For 3 disks, minimum moves = 2^3 - 1 = 8 - 1 = 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    2^3 - 1 = 7 [OK]
Hint: Use formula 2^n - 1 for minimum moves [OK]
Common Mistakes:
  • Forgetting to subtract 1
  • Using n instead of 2^n
  • Confusing number of disks with moves
2. Which of the following Go function signatures correctly represents the Tower of Hanoi recursive function?
easy
A. func towerOfHanoi(n string, from int, to int, aux int)
B. func towerOfHanoi(n int, from string, to string, aux string)
C. func towerOfHanoi(n int, from int, to int)
D. func towerOfHanoi(n int, from string, to string)

Solution

  1. Step 1: Identify parameters needed for Tower of Hanoi

    We need number of disks (int), and names of three rods (strings): from, to, auxiliary.
  2. Step 2: Match function signature

    func towerOfHanoi(n int, from string, to string, aux string) matches with n as int and three string rods. Others miss parameters or have wrong types.
  3. Final Answer:

    func towerOfHanoi(n int, from string, to string, aux string) -> Option B
  4. Quick Check:

    Correct parameters and types = func towerOfHanoi(n int, from string, to string, aux string) [OK]
Hint: Tower of Hanoi needs 4 params: disks and 3 rods [OK]
Common Mistakes:
  • Using wrong parameter types
  • Missing auxiliary rod parameter
  • Using fewer than 4 parameters
3. What will be the output of the following Go code snippet for Tower of Hanoi with 2 disks?
func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 1 {
        fmt.Printf("Move disk 1 from %s to %s\n", from, to)
        return
    }
    towerOfHanoi(n-1, from, aux, to)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}

func main() {
    towerOfHanoi(2, "A", "C", "B")
}
medium
A. Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C
B. Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B
C. Move disk 2 from A to C Move disk 1 from A to B Move disk 1 from B to C
D. Move disk 1 from B to C Move disk 2 from A to C Move disk 1 from A to B

Solution

  1. Step 1: Trace recursive calls for n=2

    First move disk 1 from A to B, then disk 2 from A to C, then disk 1 from B to C.
  2. Step 2: Match output with options

    Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C matches the exact sequence of moves printed by the code.
  3. Final Answer:

    Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C -> Option A
  4. Quick Check:

    Correct move sequence for 2 disks = Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C [OK]
Hint: Trace recursion carefully for small n [OK]
Common Mistakes:
  • Swapping auxiliary and target rods
  • Printing moves in wrong order
  • Confusing disk numbers
4. Identify the error in the following Tower of Hanoi Go function and select the correct fix:
func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 0 {
        return
    }
    towerOfHanoi(n-1, from, to, aux)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}
medium
A. Remove base case entirely
B. Change base case to if n == 1 and adjust recursive calls accordingly
C. Swap 'to' and 'aux' in first recursive call
D. Change print statement to use 'aux' instead of 'to'

Solution

  1. Step 1: Analyze recursive calls and base case

    Base case with n==0 is okay, but first recursive call uses wrong rod order: it should move disks from 'from' to 'aux' using 'to' as auxiliary.
  2. Step 2: Correct first recursive call

    Swap 'to' and 'aux' in first recursive call to towerOfHanoi(n-1, from, aux, to).
  3. Final Answer:

    Swap 'to' and 'aux' in first recursive call -> Option C
  4. Quick Check:

    Correct rod order in recursion = Swap 'to' and 'aux' in first recursive call [OK]
Hint: First recursive call moves disks to auxiliary rod [OK]
Common Mistakes:
  • Confusing auxiliary and target rods
  • Incorrect base case condition
  • Wrong print statement parameters
5. You want to solve the Tower of Hanoi problem for 4 disks but print only the moves where disk 1 is moved. Which modification to the Go recursive function below will achieve this?
func towerOfHanoi(n int, from string, to string, aux string) {
    if n == 1 {
        fmt.Printf("Move disk 1 from %s to %s\n", from, to)
        return
    }
    towerOfHanoi(n-1, from, aux, to)
    fmt.Printf("Move disk %d from %s to %s\n", n, from, to)
    towerOfHanoi(n-1, aux, to, from)
}
hard
A. Add an if condition to print only when n == 1 inside the function
B. Change base case to n == 0 and print only in recursive calls
C. Remove the print statement in the base case
D. Print only when from == "A" and to == "C"

Solution

  1. Step 1: Understand when disk 1 moves are printed

    Disk 1 moves are printed only in base case when n == 1.
  2. Step 2: Modify print statements to print only for disk 1

    Add an if condition around print statements to print only when n == 1, skipping other disks.
  3. Final Answer:

    Add an if condition to print only when n == 1 inside the function -> Option A
  4. Quick Check:

    Print only disk 1 moves = Add an if condition to print only when n == 1 inside the function [OK]
Hint: Print only when disk number is 1 inside recursion [OK]
Common Mistakes:
  • Removing base case print
  • Printing based on rod names instead of disk number
  • Changing base case incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard