Count numbers in a range that look the same rotated 180 degrees

Quick Overview

Count how many numbers in an inclusive range, given as strings of up to 15 digits, read the same after a 180-degree rotation. It tests enumeration by length, comparing bounds without converting to integers, and edge cases such as zero and odd-length middles.

Count numbers in a range that look the same rotated 180 degrees

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given two strings `low` and `high` that represent two integers `low` and `high` where `low <= high`, return the number of strobogrammatic numbers in the inclusive range `[low, high]`. A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). ### Function Signature ```python def strobogrammatic_in_range(low: str, high: str) -> int: ``` ### Rules - 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. - Numbers are written without leading zeros, so a number with more than one digit cannot start with 0. The number 0 itself counts. - Both bounds are included in the range. ### Constraints - `1 <= len(low), len(high) <= 15` - `low` and `high` consist only of digits. - `low <= high` as integers. - `low` and `high` have no leading zeros, except for `"0"` itself. ### Examples ```text Input: low = "50", high = "100" Output: 3 ``` The strobogrammatic numbers from 50 to 100 are 69, 88 and 96. The number 11 is below 50, and the smallest three-digit one, 101, is above 100. ```text Input: low = "0", high = "0" Output: 1 ``` The only number in the range is 0, and 0 looks the same upside down. ```text Input: low = "1", high = "1000" Output: 18 ``` Below 1000 there are 2 one-digit strobogrammatic numbers in range (1 and 8, since 0 is below `low`), 4 two-digit ones and 12 three-digit ones. The smallest four-digit one is 1001, which is above 1000.

Overview: Count how many numbers in an inclusive range, given as strings of up to 15 digits, read the same after a 180-degree rotation. It tests enumeration by length, comparing bounds without converting to integers, and edge cases such as zero and odd-length middles.

Google logo
Google
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given two strings low and high that represent two integers low and high where low <= high, return the number of strobogrammatic numbers in the inclusive range [low, high].

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

Function Signature

def strobogrammatic_in_range(low: str, high: str) -> int:

Rules

  • 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.
  • Numbers are written without leading zeros, so a number with more than one digit cannot start with 0. The number 0 itself counts.
  • Both bounds are included in the range.

Constraints

  • 1 <= len(low), len(high) <= 15
  • low and high consist only of digits.
  • low <= high as integers.
  • low and high have no leading zeros, except for "0" itself.

Examples

Input: low = "50", high = "100"
Output: 3

The strobogrammatic numbers from 50 to 100 are 69, 88 and 96. The number 11 is below 50, and the smallest three-digit one, 101, is above 100.

Input: low = "0", high = "0"
Output: 1

The only number in the range is 0, and 0 looks the same upside down.

Input: low = "1", high = "1000"
Output: 18

Below 1000 there are 2 one-digit strobogrammatic numbers in range (1 and 8, since 0 is below low), 4 two-digit ones and 12 three-digit ones. The smallest four-digit one is 1001, which is above 1000.