Data Structures & AlgorithmsModerateTCS NQT · CAT
Problem: The Hamming weight (number of 1-bits) of each integer from 0 to 15 is summed. What is the total?
- A. 32
- B. 64
- C. 24
- D. 15
Correct answer: A. 32
Each of the 4 bit positions is set in 8 of the 16 numbers, so the total is 4 × 8 = 32. 4 × 16 = 64 forgets the half, 3 bits give 24, and 15 counts the non-zero numbers.
More Coding Interview Problems questions
- Problem: A set of letters {A, B, C, D, E} is arranged in a row. How many arrangements have A immediately to the left of B?
- Problem: The number of ways to split a line of 6 people into 2 groups of 3 (groups are distinguishable by their first member, so each…
- Problem: The string "racecar" is compared with its reverse position by position. How many positions have matching characters?
- Problem: Starting with the number 1, double it and add 1 repeatedly. What is the value after the 10th doubling step (starting value 1, 10…
- Problem: The array [2, 4, 3, 1, 6, 5] is partitioned into the largest possible number of consecutive chunks such that after sorting each…
- Problem: A string of length 4 over {0, 1} has an even number of 1s. How many such strings are palindromes?