Show problems as
Classic problem #400MediumMathBinary Search

Nth Digit

Real-world version: Digit At A Spot On The Numbering Roll
Tracked in this browser only
Read Nth Digit on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A numbering machine prints a continuous paper roll. It prints 1, then 2, then 3, and so on without end, laying the characters of each number down one after another with nothing at all between numbers. The roll therefore begins 123456789101112..., so the tenth and eleventh characters are the 1 and the 0 of the number ten.

Characters are counted from 1 at the start of the roll. Given spot, return the single digit printed at that character position, as an integer from 0 to 9.

spot may run into the billions, so the roll cannot be printed out and indexed: your work must not scale with spot itself.

Examples

Example 1

Input
spot = 47
Output
8

Counting along the roll, character 47 falls inside the number 28 and is the second of its two characters.

Example 2

Input
spot = 190
Output
1

Characters 1 to 189 cover every number up to 99, so character 190 is the first character of 100.

Example 3

Input
spot = 1002
Output
0

Character 1002 falls inside the number 370, on its last character.

Constraints

  • 1 <= spot <= 2^31 - 1

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def digit_at_spot(spot: int) -> int:
Java
public int digitAtSpot(int spot)