Data Structures & AlgorithmsModerateTCS NQT · CAT
Problem: Count the integers from 1 to 64 whose binary representation has an even number of 1-bits.
- A. 31
- B. 32
- C. 33
- D. 15
Correct answer: A. 31
Among 0..63 exactly half (32) have an even popcount, including 0; removing 0 and noting 64 = 1000000 has one bit leaves 31. Including 0 gives 32, counting odd popcounts gives 33, and counting only numbers with exactly two 1-bits gives C(6, 2) = 15.
More Coding Interview Problems questions
- Problem: For the string "aaabbbbcc" with at most one replacement of any character by any other letter, what is the length of the longest…
- Problem: The array [1, 4, 3, 2] (with even length) is split into two-element pairs in any way, and the minimum of each pair is added up…
- Problem: A matrix stores the number of paths in a 3×3 grid where each cell can be entered from the left or from above, and blocked cells…
- Problem: The string "racecar" has length 7. How many of its substrings are palindromes with length at least 3? Count index ranges.
- Problem: Count the pairs (i, j) with i < j in the array [1, 5, 2, 8, 3, 5] whose absolute difference is at most 2.
- Problem: A dice-throwing game: starting at 0, each throw adds a value from 1 to 6, and the player stops as soon as the total is at least…