Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

In the Tower of Hanoi with 5 discs and 3 pegs A, B, C in a row, moves are allowed only between ADJACENT pegs (A↔B, B↔C, never A↔C directly). What is the minimum number of moves to transfer the tower from A to C?

  1. A. 31
  2. B. 62
  3. C. 121
  4. D. 242

Correct answer: D. 242

With the adjacency restriction, T(n) = 3T(n − 1) + 2 (move n − 1 discs A→C, the big disc A→B, the n − 1 back C→A, the big disc B→C, then n − 1 A→C), so T(n) = 3ⁿ − 1 = 242. 31 = 2⁵ − 1 is the unrestricted count.

More Coding Interview Problems questions

Exams that ask this topic