Build a Cyclic One-Bit-Change Ordering of All n-Bit Numbers from a Given Start

Quick Overview

Return an ordering of all n-bit numbers that begins at a given value, where every pair of neighbors, including the last and first, differs in exactly one bit. It tests bit manipulation and the ability to argue that a construction meets every constraint, including the wrap-around pair.

Build a Cyclic One-Bit-Change Ordering of All n-Bit Numbers from a Given Start

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two integers `n` and `start`, return any permutation `p` of the integers from 0 to ${2^n - 1}$ such that: - `p[0] = start`, - `p[i]` and `p[i + 1]` differ by only one bit in their binary representation, for every adjacent pair, and - the first element `p[0]` and the last element `p[-1]` also differ by only one bit. Any order that meets these conditions is accepted. ```text Input: n = 2, start = 3 Output: [3,2,0,1] ``` In binary the output is 11, 10, 00, 01. Every adjacent pair differs in one bit, and so do the last and first values (01 and 11). ```text Input: n = 3, start = 2 Output: [2,3,1,0,4,5,7,6] ``` This is one valid answer among several. Each consecutive pair differs in one bit, and so do 6 and 2 at the two ends. ```hint Solve start = 0 first Look for an ordering of all n-bit numbers in which each step flips a single bit and the last value is one bit away from 0. ``` ```hint Moving the start Ask what XOR with a fixed number does to the set of bit positions in which two values differ. ``` ### Constraints & Assumptions - `1 <= n <= 16`, so the output holds up to 65,536 values. - `0 <= start < 2**n`. - Any valid permutation is accepted; the examples show one possible answer each. ### What a Strong Answer Covers - A construction for the case `start = 0`, with an argument that every adjacent pair, and the pair formed by the last and first values, differ in one bit. - A way to make the sequence begin at `start` that keeps both the one-bit property and the permutation property, with the reason it does. - Time and memory cost relative to the output size, and why a search-based approach does not scale to `n = 16`. - Edge cases such as `n = 1`, plus the pitfall that `^` in Python is XOR and not a power. ### Follow-up Questions - How would you check in linear time that a returned list is a valid answer? - Given a value in your sequence for `start = 0`, how do you find its position without generating the list? - Could you build a cyclic ordering in which neighbors differ in exactly two bits instead? Why or why not? - How would you produce the values one at a time with constant extra memory?

Overview: Return an ordering of all n-bit numbers that begins at a given value, where every pair of neighbors, including the last and first, differs in exactly one bit. It tests bit manipulation and the ability to argue that a construction meets every constraint, including the wrap-around pair.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two integers n and start, return any permutation p of the integers from 0 to 2n−1{2^n - 1} such that:

  • p[0] = start ,
  • p[i] and p[i + 1] differ by only one bit in their binary representation, for every adjacent pair, and
  • the first element p[0] and the last element p[-1] also differ by only one bit.

Any order that meets these conditions is accepted.

Input: n = 2, start = 3
Output: [3,2,0,1]

In binary the output is 11, 10, 00, 01. Every adjacent pair differs in one bit, and so do the last and first values (01 and 11).

Input: n = 3, start = 2
Output: [2,3,1,0,4,5,7,6]

This is one valid answer among several. Each consecutive pair differs in one bit, and so do 6 and 2 at the two ends.

Constraints & Assumptions

  • 1 <= n <= 16 , so the output holds up to 65,536 values.
  • 0 <= start < 2**n .
  • Any valid permutation is accepted; the examples show one possible answer each.

What a Strong Answer Covers Guidance

  • A construction for the case start = 0 , with an argument that every adjacent pair, and the pair formed by the last and first values, differ in one bit.
  • A way to make the sequence begin at start that keeps both the one-bit property and the permutation property, with the reason it does.
  • Time and memory cost relative to the output size, and why a search-based approach does not scale to n = 16 .
  • Edge cases such as n = 1 , plus the pitfall that ^ in Python is XOR and not a power.

Follow-up Questions Guidance

  • How would you check in linear time that a returned list is a valid answer?
  • Given a value in your sequence for start = 0 , how do you find its position without generating the list?
  • Could you build a cyclic ordering in which neighbors differ in exactly two bits instead? Why or why not?
  • How would you produce the values one at a time with constant extra memory?