Reverse the Bit Order of a 32-Bit Sensor Reading
Last updated: November 29, 2026
Quick Overview
A temperature sensor in a fulfillment center sends 32-bit readings with the bits in reverse order. Given an unsigned 32-bit integer, return the integer whose bits are in the opposite order.
Amazon
November 29, 20260
0
0 solved
A temperature sensor in a fulfillment center sends 32-bit readings with the bits in reverse order. Given an unsigned 32-bit integer, return the integer whose bits are in the opposite order.
A short Amazon online assessment item that checks comfort with shifts and masks. Graders look for a correct fixed-width loop and, in follow-up, for a byte lookup table when the function is called millions of times.
What the Interviewer Expects
- Loops exactly 32 times, not until the number is 0
- Builds the result with shift and OR
- Keeps the result within 32 bits
- Mentions the lookup-table or divide-and-conquer speedup
- Tests 0 and all ones
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
- How would you speed this up if called a billion times?
- Do it with five mask-and-swap steps instead of a loop.
- How does this differ in a language with signed 32-bit integers?
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 0 <= x < 2^32. Output: the 32-bit integer with bit i of x moved to bit 31 - i. Leading zeros count, so 1 becomes 2^31....
High-Level Design
Process 32 positions: shift the result left by one, OR in the lowest bit of x, then shift x right. Stopping when x becomes 0 would be wrong because the remaining zeros still need to push the result to...