Gray Code
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A **Gray code** sequence of n bits lists every integer in [0, 2^n - 1] exactly once so that any two neighbouring values differ in exactly one bit, and the last value also differs from the first in exactly one bit.
Given n, return the **reflected binary Gray code** of width n as a list of integers. Many sequences satisfy the one-bit rule, so the output is pinned to this specific one: it starts at 0, and the sequence for n bits is the sequence for n - 1 bits followed by that same sequence in reverse order with bit n - 1 set on each value.
The result has exactly 2^n elements. Aim for O(2^n) time.
Examples
Constraints
- 1 <= n <= 16
- The output has exactly 2^n integers
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.