Generate all n-digit numbers that look the same rotated 180 degrees

Quick Overview

Generate every n-digit number that reads the same after a 180-degree rotation and return them in sorted order. It tests recursive construction from the outside in, the leading-zero rule for the outer digits, and reasoning about output size.

Generate all n-digit numbers that look the same rotated 180 degrees

Company: Cisco

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer `n`, return all the strobogrammatic numbers that have exactly `n` digits, sorted in ascending order. A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). ### Function Signature ```python def find_strobogrammatic(n: int) -> list[str]: ``` ### Rules - Return each number as a string. - Return the list sorted in ascending order. All strings have the same length, so string order and numeric order agree. - The digits 0, 1 and 8 stay the same upside down, 6 becomes 9, and 9 becomes 6. The digits 2, 3, 4, 5 and 7 have no valid upside-down form. - A number with more than one digit cannot start with 0. For `n = 1`, "0" counts. ### Constraints - `1 <= n <= 14` ### Examples ```text Input: n = 2 Output: ["11", "69", "88", "96"] ``` The two digits must turn into each other when rotated. The pair "00" is excluded because it has a leading zero. ```text Input: n = 1 Output: ["0", "1", "8"] ``` A single digit has to rotate to itself, and only 0, 1 and 8 do. ```text Input: n = 3 Output: ["101", "111", "181", "609", "619", "689", "808", "818", "888", "906", "916", "986"] ``` The outer digits are 1...1, 6...9, 8...8 or 9...6, and the middle digit is 0, 1 or 8, which gives 12 numbers (4 outer pairs times 3 middles).

Overview: Generate every n-digit number that reads the same after a 180-degree rotation and return them in sorted order. It tests recursive construction from the outside in, the leading-zero rule for the outer digits, and reasoning about output size.

Cisco logo
Cisco
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer n, return all the strobogrammatic numbers that have exactly n digits, sorted in ascending order.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Function Signature

def find_strobogrammatic(n: int) -> list[str]:

Rules

  • Return each number as a string.
  • Return the list sorted in ascending order. All strings have the same length, so string order and numeric order agree.
  • The digits 0, 1 and 8 stay the same upside down, 6 becomes 9, and 9 becomes 6. The digits 2, 3, 4, 5 and 7 have no valid upside-down form.
  • A number with more than one digit cannot start with 0. For n = 1 , "0" counts.

Constraints

  • 1 <= n <= 14

Examples

Input: n = 2
Output: ["11", "69", "88", "96"]

The two digits must turn into each other when rotated. The pair "00" is excluded because it has a leading zero.

Input: n = 1
Output: ["0", "1", "8"]

A single digit has to rotate to itself, and only 0, 1 and 8 do.

Input: n = 3
Output: ["101", "111", "181", "609", "619", "689", "808", "818", "888", "906", "916", "986"]

The outer digits are 1...1, 6...9, 8...8 or 9...6, and the middle digit is 0, 1 or 8, which gives 12 numbers (4 outer pairs times 3 middles).