All problems
0458MediumMathBinary Search

Digit At A Spot On The Numbering Roll

Tracked in this browser only
Write code

Trains the technique from

LeetCode 400Nth Digit

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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