Data Structures & AlgorithmsHardAmazon · Microsoft OA

A binary counter starts at 0 and is incremented n times; each increment flips trailing 1s to 0 and one 0 to 1. What is the total number of bit flips?

  1. A. Θ(n log n), since each increment can flip log n bits
  2. B. Less than 2n, so O(1) amortised per increment
  3. C. Exactly n
  4. D. Θ(n²)

Correct answer: B. Less than 2n, so O(1) amortised per increment

Bit i flips every 2ⁱ increments, so the total is Σ n/2ⁱ < 2n. A single increment like 0111…1 → 1000…0 is expensive, but such increments are rare.

More Coding Interview Problems questions

Exams that ask this topic