Order Rotary Encoder Positions So Adjacent Readings Differ by One Bit
Last updated: November 29, 2026
Quick Overview
A warehouse conveyor's rotary encoder reports an n-bit position, and to avoid misreads between ticks consecutive positions must differ in exactly one bit, including the wrap from last to first. Return such a cyclic sequence of all 2^n positions starting from 0.
Amazon
November 29, 20260
0
0 solved
A warehouse conveyor's rotary encoder reports an n-bit position, and to avoid misreads between ticks consecutive positions must differ in exactly one bit, including the wrap from last to first. Return such a cyclic sequence of all 2^n positions starting from 0.
Appears in Amazon technical screens for robotics and devices teams. The interviewer wants either the reflect-and-prefix construction or the closed form i ^ (i >> 1), plus a short proof that neighbors differ in one bit.
What the Interviewer Expects
- Produces a valid Gray code of length 2^n
- Uses either reflection or i ^ (i >> 1)
- Explains why adjacent codes differ by one bit
- Checks the wrap-around property
- Handles n = 0
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Given a Gray code value, recover its index.
- Start the cycle from an arbitrary position s instead of 0.
- Why do real encoders prefer Gray code over plain binary?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: integer n >= 0. Output: a list of 2^n distinct integers in [0, 2^n), starting at 0, where each adjacent pair (and the last and first) differ in exactly one bit....
High-Level Design
Reflection: the Gray code for n bits is the code for n - 1 bits, followed by the same list reversed with the top bit set. Inside each half neighbors differ by one bit by induction, and at the seam the...