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?- A. 6
- B. 4
- C. 7
- 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
- Problem: How many ways are there to make 15 from unlimited coins of value 1, 5 and 10 (order does not matter)? Run the DP. python ways =…
- Problem: How many integers in [1, 100000] are multiples of 7 whose last digit is 3? Run the code. python print(sum(1 for x in range(1…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 12 satisfy x^2 + y^2 <= 100? Run the code. python print(sum(1 for x in range(1…
- Problem: How many lattice points (x, y) with x >= 0, y >= 0 and x + y <= 10 lie in the triangle? Run the code. python print(sum(1 for x in…
- Problem: What is the sum of the ASCII codes of the characters of 'hello'? Run the code. python print(sum(ord(c) for c in 'hello')) What is…
- Problem: What is the length of the longest strictly increasing contiguous subarray of [3, 4, 5, 1, 2, 3, 4, 5, 6]? Run the code. python a…