Bird
Raised Fist0

What is the output of the following code snippet when called with grayCode(1)?

medium🧾 Code Trace Q4 of Q15
Bit Manipulation - Gray Code
What is the output of the following code snippet when called with grayCode(1)?
A[1, 1]
B[0, 2]
C[1, 0]
D[0, 1]
Step-by-Step Solution
Solution:
  1. Step 1: Compute for n=1

    Range is 0 to 1. Compute i ^ (i >> 1): 0^(0)=0, 1^(0)=1.
  2. Step 2: Collect results

    The sequence is [0, 1].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Matches Gray code for 1 bit [OK]
Quick Trick: Gray code for 1 bit is [0,1] [OK]
Common Mistakes:
MISTAKES
  • Assuming output starts at 1
  • Misapplying shift for small n
Trap Explanation:
PITFALL
  • Candidates sometimes confuse the base case or output order for small inputs.
Interviewer Note:
CONTEXT
  • Checks handling of edge cases and small inputs in bitwise code.
Master "Gray Code" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes