The Rotated Digits interview question asks you to count how many numbers in the range [1, n] are "good." A number is good if, when each of its digits is rotated 180°, the result is a valid number that is DIFFERENT from the original. Digits that remain valid after rotation: 0→0, 1→1, 2→5, 5→2, 6→9, 8→8, 9→6. Digits that become invalid: 3, 4, 7. A number is good if all its digits rotate to valid digits AND at least one digit changes (2, 5, 6, or 9 must appear).
Uber, Arista Networks, Meta, Amazon, and Google ask this problem because it applies per-digit validation logic with a classification rule. It has a dynamic programming optimization: for larger n, digit DP can count valid numbers without checking each one individually. It evaluates candidates' ability to break a mathematical condition into per-digit classification.
The simple pattern is digit-by-digit validation. For each number i in [1, n], check all digits: if any digit is in {3, 4, 7}, it's invalid. If all digits are in {0,1,2,5,6,8,9} and at least one is in {2,5,6,9} (it changes), it's good. The DP pattern: classify each digit as 0 (invalid), 1 (valid, unchanged), or 2 (valid, changes). Build prefix counts using digit DP for O(log n) time.
n = 10. Check 1–10:
Count: 4 (numbers 2, 5, 6, 9).
For the Rotated Digits coding problem, the math and dynamic programming interview pattern applies for larger inputs. For the interview, the simple O(n log n) digit-check per number is usually acceptable given n ≤ 10,000. Know the classification: {3,4,7} = invalid, {0,1,8} = valid/unchanged, {2,5,6,9} = valid/changed. Google interviewers may ask for the digit DP approach for larger n — be ready to outline the DP state.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| 2 Keys Keyboard | Medium | Solve | |
| Integer Break | Medium | Solve | |
| Egg Drop With 2 Eggs and N Floors | Medium | Solve | |
| 4 Keys Keyboard | Medium | Solve | |
| Number of Ways to Build House of Cards | Medium | Solve |