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?
- A. 31
- B. 62
- C. 121
- 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
- Problem: Count the integers in [1, 1000] that are coprime to 1000. python from math import gcd print(sum(1 for i in range(1, 1001) if…
- Reservoir sampling keeps k = 2 items from a stream of n = 10 items: the first 2 fill the reservoir, and item i (i > 2) is admitted with…
- Problem: An LFU cache (capacity 2) evicts the least frequently used key; ties are broken by least recently used. get returns −1 on a miss…
- Edge case: A generator expression is consumed twice. python g = (x x for x in range(4)) print(sum(g), sum(g)) What is printed?
- Problem: Sort students by grade (descending), breaking ties by name (ascending), using two passes of Python's STABLE sort. python s =…
- Problem: Find the length of the SHORTEST subarray with sum ≥ k when values may be negative (monotonic deque over prefix sums). python from…