Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the number of set bits in 2^30 - 1? Run the code.
Problem: What is the number of set bits in 2^30 - 1? Run the code.
print(bin(2 ** 30 - 1).count('1'))What is printed?- A. 31
- B. 29
- C. 1
- D. 30
Correct answer: D. 30
2³⁰ − 1 is thirty 1s in binary (111…1), so popcount = 30. 31 counts the bits of 2³⁰, 29 is an off-by-one on the exponent, and 1 is the popcount of 2³⁰ itself.
More Coding Interview Problems questions
- Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code. python print(sum(1 for x in range(1, 301) if x % 6…
- Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code. python from math…
- Problem: What is the complexity of a single query in a sparse-table range minimum structure?
- Problem: What is the purpose of a 'suffix link' in Ukkonen's or Aho-Corasick's construction?
- Problem: Which property does an 'order statistics' tree support in addition to search?