Generate Every Number That Reads the Same Upside Down
Last updated: November 29, 2026
Quick Overview
Return all numbers of exactly n digits (no leading zero unless n is 1) that look identical when rotated 180 degrees, using the pairs 0-0, 1-1, 6-9, 8-8, and 9-6.
0
0
0 solved
Return all numbers of exactly n digits (no leading zero unless n is 1) that look identical when rotated 180 degrees, using the pairs 0-0, 1-1, 6-9, 8-8, and 9-6.
A Google-flavored recursion question (strobogrammatic numbers) that checks whether you build answers from the outside in instead of filtering all n-digit numbers. Interviewers look for correct handling of the middle digit and the leading-zero rule, then shift constraints to counting in a range.
What the Interviewer Expects
- Builds numbers outward with mirrored pairs instead of brute-force filtering
- Handles odd n with a self-symmetric middle digit (0, 1, 8)
- Excludes leading zeros for n greater than 1 only at the outermost layer
- Analyzes output size, roughly 5^(n/2)
- Explains how to count in a range without generating
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
- Count such numbers between two bounds low and high given as strings.
- Check whether a given string is strobogrammatic in O(n).
- Which digits change if the display is a seven-segment clock?
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 n at least 1.
- Output: list of strings of length n that equal their 180-degree rotation.
- Leading zeros not allowed except for the single number "0"....
High-Level Design
Recursively build the inner core of length m for m = n, n - 2, ... down to 0 or 1. Base cases: length 0 gives [""], length 1 gives ["0", "1", "8"] (digits that map to themselves). For length m, ta...