Ctrl K

One-Bit Step Ring

Medium
We want to write all numbers from 0 to 2^bits − 1 around a circle so that any two neighbours (including the last and the first) differ in exactly one bit. We use the **mirror order** for that: - for 1 bit it is 0, 1; - the order for b bits is built from the order for b − 1 bits: first that order itself, then the same numbers in reverse with bit b − 1 (the new top bit) switched on. For example, 2 bits give 0, 1, 3, 2. Return the 2^bits numbers in mirror order, rotated around the circle so that the list starts with start.
Example 1
bits=2, start=1 → [1, 3, 2, 0]

The mirror order for 2 bits is 00, 01, 11, 10, i.e. 0, 1, 3, 2. Starting from 1 and wrapping around gives 1, 3, 2, 0. Every neighbouring pair, including 0 and 1 at the seam, differs in one bit.

Example 2
bits=3, start=6 → [6, 7, 5, 4, 0, 1, 3, 2]

For 3 bits, take 0, 1, 3, 2 and append the same values in reverse with the top bit set: 6, 7, 5, 4. That gives 0, 1, 3, 2, 6, 7, 5, 4, rotated so that 6 comes first.

Example 3
bits=1, start=0 → [0, 1]
Constraints
  • 1 ≤ bits ≤ 16
  • 0 ≤ start < 2^bits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirrored One-Bit Codes MediumFewest Lines Through the Dots MediumSquare-Sum Chains HardXOR of Every Subset EasyDivision Without Dividing Medium
loading Python…
Input
bits=2, start=1
Expected
[1, 3, 2, 0]

Run checks the first 3 tests in your browser. Submit checks all 58 tests on the server, hidden ones included.