Find a Digit in the Concatenated Positive Integers

Quick Overview

Practice locating a one-based digit in the concatenation of positive decimal integers. Handle number-length boundaries and large positions without constructing the sequence.

Find a Digit in the Concatenated Positive Integers

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Find a Digit in the Concatenated Positive Integers Write the positive integers consecutively in decimal: ```text 123456789101112131415... ``` Given `n`, return the digit at the one-based position `n` in this infinite digit sequence. ### Function `int findNthDigit(int n)` ### Example 1 ```text Input: n = 15 Output: 2 ``` Positions `14` and `15` are the digits of `12`, so position `15` contains `2`. ### Example 2 ```text Input: n = 190 Output: 1 ``` The one-digit integers contribute `9` digits, and the two-digit integers contribute `180`. Position `190` is the first digit of `100`. ### Example 3 ```text Input: n = 1 Output: 1 ``` ### Constraints - `1 <= n <= 2147483647`. ### Clarifications Positions count individual digits, not integers. Return a digit as an integer from `0` through `9`, not a string or the whole number containing it. ### What a Strong Answer Covers - One-based positioning and boundaries between decimal lengths. - Selecting the exact digit within its containing number. - Correctness and safe arithmetic near the maximum position. - Time and storage bounds.

Overview: Practice locating a one-based digit in the concatenation of positive decimal integers. Handle number-length boundaries and large positions without constructing the sequence.

Google logo
Google
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Find a Digit in the Concatenated Positive Integers

Write the positive integers consecutively in decimal:

123456789101112131415...

Given n, return the digit at the one-based position n in this infinite digit sequence.

Function

int findNthDigit(int n)

Example 1

Input: n = 15
Output: 2

Positions 14 and 15 are the digits of 12, so position 15 contains 2.

Example 2

Input: n = 190
Output: 1

The one-digit integers contribute 9 digits, and the two-digit integers contribute 180. Position 190 is the first digit of 100.

Example 3

Input: n = 1
Output: 1

Constraints

  • 1 <= n <= 2147483647 .

Clarifications

Positions count individual digits, not integers. Return a digit as an integer from 0 through 9, not a string or the whole number containing it.

What a Strong Answer Covers Guidance

  • One-based positioning and boundaries between decimal lengths.
  • Selecting the exact digit within its containing number.
  • Correctness and safe arithmetic near the maximum position.
  • Time and storage bounds.