Check whether a digit string looks the same rotated 180 degrees

Quick Overview

Decide whether a string of digits reads the same after it is turned upside down. It tests mapping each digit to its rotated form, comparing positions from both ends of a string, and edge cases such as a 6 or 9 in the middle.

Check whether a digit string looks the same rotated 180 degrees

Company: Cisco

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `num` that represents an integer, return `True` if `num` is a strobogrammatic number and `False` otherwise. A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). ### Function Signature ```python def is_strobogrammatic(num: str) -> bool: ``` ### Rules - Rotating a number by 180 degrees reverses the order of its digits and turns each digit upside down. - 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 are not valid digits upside down, so a string that contains any of them is not strobogrammatic. ### Constraints - `1 <= len(num) <= 50` - `num` consists only of digits. - `num` has no leading zeros, except for `"0"` itself. ### Examples ```text Input: num = "69" Output: True ``` Rotating "69" reverses it to "96" and then flips each digit (9 becomes 6, 6 becomes 9), which gives "69" again. ```text Input: num = "88" Output: True ``` Each 8 stays an 8 upside down, so the rotated string is "88". ```text Input: num = "962" Output: False ``` The digit 2 has no upside-down form. The outer pair fails on its own as well: a 9 at the front would need a 6 at the end.

Overview: Decide whether a string of digits reads the same after it is turned upside down. It tests mapping each digit to its rotated form, comparing positions from both ends of a string, and edge cases such as a 6 or 9 in the middle.

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

Given a string num that represents an integer, return True if num is a strobogrammatic number and False otherwise.

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

Function Signature

def is_strobogrammatic(num: str) -> bool:

Rules

  • Rotating a number by 180 degrees reverses the order of its digits and turns each digit upside down.
  • 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 are not valid digits upside down, so a string that contains any of them is not strobogrammatic.

Constraints

  • 1 <= len(num) <= 50
  • num consists only of digits.
  • num has no leading zeros, except for "0" itself.

Examples

Input: num = "69"
Output: True

Rotating "69" reverses it to "96" and then flips each digit (9 becomes 6, 6 becomes 9), which gives "69" again.

Input: num = "88"
Output: True

Each 8 stays an 8 upside down, so the rotated string is "88".

Input: num = "962"
Output: False

The digit 2 has no upside-down form. The outer pair fails on its own as well: a 9 at the front would need a 6 at the end.