Data Structures & AlgorithmsHardCAT · Product Company SDE
Problem: How many integers x in the range 1 ≤ x ≤ 1000 have exactly 4 set bits in their binary representation?
- A. 120
- B. 385
- C. 210
- D. 252
Correct answer: C. 210
Every number from 1000 to 1023 starts with 11111 and has at least 5 set bits, so the count equals the 10-bit count C(10, 4) = 210. Counting exactly 3 bits gives C(10, 3) = 120, exactly 5 bits gives 252, and 'at most 4 bits' gives 385.
More Coding Interview Problems questions
- Problem: A staircase can be climbed by steps of size 1, 3 or 4. How many distinct ordered sequences of steps climb exactly 13 stairs?
- Problem: What is the length of the longest strictly increasing subsequence of the sequence 10, 9, 2, 5, 3, 7, 101, 18, 4, 6, 8, 1, 12?
- Problem: Coins of denominations 5, 12 and 20 are available in unlimited supply. What is the minimum number of coins needed to make exactly…
- Problem: The list [25, 14, 9, 31, 7, 18, 3, 12] is turned into a min-heap in place with heapify (standard bottom-up sift-down, 0-indexed)…
- Problem: The sorted array [2, 4, 4, 4, 7, 9, 9, 12] is searched with bisectleft and bisectright. What is bisectright(a, 4) − bisectleft(a…
- Problem: Compute (7^2026 + 11^2026) mod 1000, where ^ is exponentiation.