Confusing Number
What is this problem about?
The Confusing Number interview question defines a "confusing number" as one that, when rotated 180 degrees, results in a different valid number.
- Valid digits when rotated: 0, 1, 6, 8, 9.
- Rotations: 0->0, 1->1, 6->9, 8->8, 9->6.
- All other digits (2, 3, 4, 5, 7) make the number invalid.
A number is "confusing" if it consists only of valid digits AND the resulting rotated number is not equal to the original number.
Why is this asked in interviews?
Google asks the Math interview pattern to test your ability to handle digit-by-digit manipulation and basic logic. It’s a simple problem that checks if you can reverse a number while applying a mapping to each digit. It evaluates your attention to detail regarding the "different from original" rule.
Algorithmic pattern used
The approach is a simple Simulation.
- Create a map for the valid rotations.
- Iterate through the digits of the number (either by converting to string or using
% 10 and / 10).
- If any digit is not in the map, it's not confusing. Return false.
- Build the rotated number by taking the mapped digit and adding it to
rotated_num * 10.
- Check if
rotated_num != original_num.
Example explanation
N = 6
- Digit 6 is valid. Rotation is 9.
- Rotated number is 9.
- 9 != 6.
Result: True (It's a confusing number).
N = 89
- Digit 9 rotates to 6.
- Digit 8 rotates to 8.
- Rotated number is 68.
- 68 != 89.
Result: True.
N = 11
- Digit 1 rotates to 1.
- Rotated number is 11.
- 11 == 11.
Result: False.
Common mistakes candidates make
- Invalid digits: Forgetting to check if all digits are rotatable.
- Leading zeros: Not realizing that if a number ends in 0 (like 10), the rotation (01) is just 1, which is a valid integer.
- Identity check: Forgetting the condition that the rotated number must be different from the original.
Interview preparation tip
Always clarify if the rotated number can have leading zeros. In this problem, "01" is treated as the integer "1".