Ctrl K

Digit of Endless Counting

Medium
Write all positive integers one after another with no separators: 123456789101112131415… and on forever. Given position, return the digit that sits at that position of the string (positions start at 1). Building the string itself is not an option: the position can be around two billion.
Example 1
position=7 → 7

The first nine digits are just the numbers 1 to 9, so the seventh one is 7.

Example 2
position=12 → 1

The string starts 123456789101112… Digits 10–11 form the number 10, and the 12th is the first digit of 11, which is 1.

Example 3
position=190 → 1

One-digit numbers give 9 digits and two-digit numbers another 180, 189 in total. So the 190th digit is the first digit of 100.

Constraints
  • 1 ≤ position ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Squares Make It MediumGrowing Steps on a Line MediumN-th Number Divisible by One of Three MediumPrices That Fade With Each Sale MediumFencing an Orchard Plot Medium
loading Python…
Input
position=7
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 53 tests on the server, hidden ones included.