Data Structures & AlgorithmsModerateTCS NQT · CAT
Problem: How many bits are needed to write 10^9 in binary (the number of bits in its binary representation)?
- A. 29
- B. 10
- C. 32
- D. 30
Correct answer: D. 30
2²⁹ = 536870912 ≤ 10⁹ < 2³⁰, so the bit length is floor(log₂ n) + 1 = 30. Forgetting the + 1 gives 29, 10 is the decimal digit count, and 32 is the size of an int, not the bit length.
More Coding Interview Problems questions
- Problem: Reverse the 8-bit binary representation of 13 (00001101) and report the resulting unsigned value in decimal.
- Problem: What is the smallest positive integer x such that 1 + 2 + … + x is at least 100?
- Problem: How many composite numbers lie in the range 2 to 50 (inclusive)?
- Problem: How many integers x with 0 ≤ x ≤ 99 satisfy x³ ≡ x (mod 7)?
- Problem: Daily temperatures are [73, 74, 75, 71, 69, 72, 76, 73]. For each day, the wait is the number of days until a strictly warmer day…
- Problem: In the binary array [1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1] at most 2 zeros may be flipped to 1. What is the length of the longest run…