Guess the Number Using Bitwise Questions II - Problem

Imagine you're playing an interactive guessing game where you need to find a secret number n between 0 and 230 - 1. However, there's a twist that makes this challenge particularly intriguing!

You have access to a special API function commonBits(num) that helps you gather information about the secret number. But here's the catch: every time you call this function, the secret number changes! Your mission is to find the original value of n before any modifications.

The commonBits(num) function works as follows:

  1. It calculates how many bit positions have the same value in both n and your input num
  2. It then performs n = n XOR num (modifying the secret number!)
  3. Finally, it returns the count of matching bits

Goal: Determine the initial value of the secret number n using strategic calls to commonBits().

Note: Only the first 30 bits are considered when counting common bits, as all numbers are guaranteed to be less than 230.

Input & Output

example_1.py โ€” Python
$ Input: Secret number n = 5 (binary: 101)
โ€บ Output: 5
๐Ÿ’ก Note: We query each bit: commonBits(1) returns 1 (odd, so bit 0 = 1), commonBits(2) returns 0 (even, so bit 1 = 0), commonBits(4) returns 1 (odd, so bit 2 = 1). Result: 101 binary = 5 decimal.
example_2.py โ€” Python
$ Input: Secret number n = 0 (binary: 000...000)
โ€บ Output: 0
๐Ÿ’ก Note: All calls to commonBits(2^i) will return 0 (even), indicating all bits are 0. Result: 0.
example_3.py โ€” Python
$ Input: Secret number n = 1073741823 (binary: 111...111, all 30 bits set)
โ€บ Output: 1073741823
๐Ÿ’ก Note: All calls to commonBits(2^i) will return 1 (odd), indicating all 30 bits are 1. Result: 2^30 - 1 = 1073741823.

Visualization

Tap to expand
XOR Magic: How Bit Discovery WorksExample: Original n = 13 (binary: 1101)1101Bit 3 2 1 0Discovery Process:Query bit 0: commonBits(0001) โ†’ Compare 1101 & 0001 โ†’ 1 match (odd) โ†’ bit 0 = 1 โœ“Query bit 1: commonBits(0010) โ†’ Compare ???? & 0010 โ†’ 0 matches (even) โ†’ bit 1 = 0 โœ“Query bit 2: commonBits(0100) โ†’ Compare ???? & 0100 โ†’ 1 match (odd) โ†’ bit 2 = 1 โœ“Query bit 3: commonBits(1000) โ†’ Compare ???? & 1000 โ†’ 1 match (odd) โ†’ bit 3 = 1 โœ“Reconstructed: 1101 = 13 โœ“Each query reveals one bit, regardless of how n changes!
Understanding the Visualization
1
Initial State
Secret number n has unknown bits
2
Query Single Bit
Ask about one bit position using power of 2
3
Analyze Response
Odd count means bit was 1, even means bit was 0
4
Reconstruct
Combine all discovered bits to get original number
Key Takeaway
๐ŸŽฏ Key Insight: By querying single bit positions, we isolate each bit's contribution to the XOR operation, allowing us to determine the original number bit by bit!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(log n)

We make exactly 30 calls (one for each bit position)

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only need to store the result and bit position

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค n โ‰ค 230 - 1
  • You can make at most 30 calls to commonBits()
  • Only the first 30 bits are considered for bit comparison
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 15
43.6K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen