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?
- A. 6
- B. 5
- C. 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
- Problem: A permissions engine isolates bits of a mask. python x = 88 print(x & -x, x & (x - 1)) What is printed?
- Problem: Rearrange the digits of a version list into the next lexicographically greater permutation. python def nxt(a): i = len(a) - 2…
- An analytics service holds a static array of n = 10⁵ integers and must answer q = 10⁶ queries 'minimum of a[l..r]' as fast as possible…
- Given n = 5 × 10⁴ integers, count pairs i < j with a[i] > 2·a[j]. Which approach is correct and efficient?
- Problem: Evaluate a reverse-Polish expression. The specification says division must truncate toward zero (as in C or Java). python def…
- Problem: Sum the minimum of every contiguous subarray using a monotonic stack (previous-less and next-less-or-equal boundaries). python…