Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard

Problem: The reflected binary Gray code of i is i ^ (i >> 1). At which position of the 3-bit Gray sequence does the code 6 (110) appear?

Problem: The reflected binary Gray code of i is i ^ (i >> 1). At which position of the 3-bit Gray sequence does the code 6 (110) appear?
seq = [i ^ (i >> 1) for i in range(8)]
print(seq.index(6))
What is printed?
  1. A. 6
  2. B. 5
  3. C. 4
  4. D. 3

Correct answer: C. 4

The sequence is 0, 1, 3, 2, 6, 7, 5, 4, so 6 is at index 4. Equivalently, decoding Gray 110 gives binary 1, 1⊕1 = 0, 0⊕0 = 0, i.e. 100 = 4.

More Coding Interview Problems questions

Exams that ask this topic