Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A Gray code of 4 bits is generated as g = n XOR (n >> 1) for n = 0 to 15. What is the Gray code value of n = 11?
- A. 29
- B. 5
- C. 14
- D. 13
Correct answer: C. 14
11 = 1011 and 11 >> 1 = 0101, so g = 1011 XOR 0101 = 1110 = 14. Shifting left instead (11 XOR 22) gives 29, reporting n >> 1 alone gives 5, and decoding 11 as if it were already a Gray code gives 13.
More Coding Interview Problems questions
- Problem: The sum of the digits of 2^100 written in base 10 is computed. What is that sum?
- Problem: In a spiral traversal of a 5×5 matrix filled row-wise with 1 to 25, the traversal starts at the top-left and goes right, then…
- Problem: A bitmask DP problem: count the subsets of {1, 2, ..., 10} whose sum is a multiple of 5 (the empty set counts, since its sum 0 is…
- Problem: An 8-bit unsigned counter starts at 250 and is incremented by 7 each step, wrapping modulo 256. How many increments until the…
- Problem: The Pascal triangle rows are numbered from row 0 (single 1). What is the sum of all entries in row 12 that are odd?
- Problem: Count the strings of length 4 over the alphabet {a, b, c} that contain no two adjacent equal characters and do not contain the…