Ctrl K

Mirrored One-Bit Codes

Medium
A rotary sensor is split into 2^bits sectors, and each sector carries a code from 0 to 2^bits - 1. So the sensor never misreads at a border, the codes of neighboring sectors (including the last and the first) must differ in exactly one bit. Such a sequence is built by mirroring: 1. start with the list [0]; 2. for each bit k = 0, 1, …, bits - 1, append the current list in reverse order with bit k switched on in every number. Return the resulting list of 2^bits numbers in exactly this order.
Example 1
bits=2 → [0, 1, 3, 2]

Start with [0]. Bit 0: the mirror of [0] with bit 0 set gives [0, 1]. Bit 1: the mirror [1, 0] with bit 1 set gives [3, 2], so the result is [0, 1, 3, 2], or 00, 01, 11, 10 in binary.

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

Append [0, 1, 3, 2] in reverse with bit 2 switched on: 2+4, 3+4, 1+4, 0+4.

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

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