Rotated Digits

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Rotating a digit by 180 degrees turns 0, 1, and 8 into themselves, turns 2 into 5 and 5 into 2, and turns 6 into 9 and 9 into 6. The digits 3, 4, and 7 become invalid when rotated. Rotating an integer means rotating every one of its digits in place. Call an integer **good** if every digit stays valid after rotation **and** the rotated number is different from the original. Given n, return how many integers in the range [1, n] are good.

Examples

Input: n = 10
Output: 4
The good numbers are 2, 5, 6, and 9. `1` and `10` rotate to themselves.
Input: n = 1
Output: 0
`1` rotates to itself.

Constraints

  • `1 <= n <= 10^4`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems