Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the longest run of consecutive 1 bits in the binary representation of 1000? Run the code.

Problem: What is the longest run of consecutive 1 bits in the binary representation of 1000? Run the code.
x = 1000
best = cur = 0
for ch in bin(x)[2:]:
    cur = cur + 1 if ch == '1' else 0
    best = max(best, cur)
print(best)
What is printed?
  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

The binary form of 1000 is 1111101000, and its longest block of ones is found by a single scan.

More Coding Interview Problems questions

Exams that ask this topic