Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: Tower of Hanoi with 6 disks, moving all disks from peg A to peg C using peg B as the spare (standard rules). How many moves does the smallest disk (disk 1) make in total?
- A. 63
- B. 32
- C. 64
- D. 16
Correct answer: B. 32
Disk k moves 2^(n−k) times, so the smallest disk moves 2⁵ = 32 times (every other move). The total move count is 2⁶ − 1 = 63, 2⁶ = 64 is off by one, and 16 is the move count of disk 2.
More Coding Interview Problems questions
- Problem: Count the integers from 1 to 999 whose decimal digit sum is divisible by 7.
- Problem: Compute Euler's totient φ(n) summed over n = 1 to 30 (φ(n) counts integers in [1, n] coprime to n, with φ(1) = 1).
- Problem: How many positive divisors does 720 have?
- Problem: Find the modular inverse of 7 modulo 26 (the value x with 0 ≤ x < 26 such that 7x ≡ 1 mod 26).
- Problem: A Gray code of 4 bits is generated as g = n XOR (n >> 1) for n = 0 to 15. What is the Gray code value of n = 11?
- Problem: The sum of the digits of 2^100 written in base 10 is computed. What is that sum?