Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
Problem: The infinite string 123456789101112… is indexed from 1. Find the n-th digit by skipping whole blocks of 1-digit, 2-digit, … numbers.
Problem: The infinite string 123456789101112… is indexed from 1. Find the n-th digit by skipping whole blocks of 1-digit, 2-digit, … numbers.
def nth(n):
length, count, start = 1, 9, 1
while n > length * count:
n -= length * count; length += 1; count *= 10; start *= 10
num = start + (n - 1) // length
return int(str(num)[(n - 1) % length])
print(nth(11), nth(190), nth(1000))What is printed?- A. 1 0 9
- B. 0 9 3
- C. 1 1 3
- D. 0 1 3
Correct answer: D. 0 1 3
Digits 1–9 are one-digit numbers; 10–99 contribute 180 digits (positions 10–189). n = 11 is the second digit of 10 → 0. n = 190 is the first digit of 100 → 1. n = 1000: subtract 9 and 180 → 811th digit among 3-digit numbers: number 100 + 270 = 370, digit index 0 → 3.
More Coding Interview Problems questions
- Problem: Compute a^b mod 1337 where the exponent b is given as a digit array (too large for an integer), using a^(10x + d) = (a^x)^10 ·…
- A stack supports push, pop and multipop(k) (pop up to k elements). Starting from empty, what is the worst-case total cost of any sequence…
- A binary counter starts at 0 and is incremented n times; each increment flips trailing 1s to 0 and one 0 to 1. What is the total number of…
- A service must detect whether a dependency graph with 10⁵ modules and 10⁶ edges has a cycle. Which approach is both correct and efficient?
- Shortest paths in a large grid where each move costs either 0 or 1. Which algorithm is the most efficient correct choice?
- You must report the 100 largest values from a stream of 10⁹ integers using about 1 MB of memory. Which approach fits?