Count Decimal One Digits up to a Bound

Quick Overview

Count Decimal One Digits up to a Bound. Count all appearances of decimal digit 1 in the ordinary spellings of integers from 0 through n, inclusive. Count occurrences, not merely how many numbers conta

Count Decimal One Digits up to a Bound

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Count all appearances of decimal digit 1 in the ordinary spellings of integers from 0 through n, inclusive. Count occurrences, not merely how many numbers contain a 1. ```cpp int countDigitOne(int n); ``` ### Examples ```text n = 20 -> 12 n = 101 -> 23 ``` ### Constraints - 0 <= n <= 1000000000.

Overview: Count Decimal One Digits up to a Bound. Count all appearances of decimal digit 1 in the ordinary spellings of integers from 0 through n, inclusive. Count occurrences, not merely how many numbers conta

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Count all appearances of decimal digit 1 in the ordinary spellings of integers from 0 through n, inclusive. Count occurrences, not merely how many numbers contain a 1.

int countDigitOne(int n);

Examples

n = 20 -> 12
n = 101 -> 23

Constraints

  • 0 <= n <= 1000000000.