Data Structures & AlgorithmsVery hardInfosys SP/DSE · Product Company SDE
Problem: Find how many numbers in 1..n have all distinct digits — the digit-DP-free shortcut for n = 10ᵏ uses counting.
Problem: Find how many numbers in 1..n have all distinct digits — the digit-DP-free shortcut for n = 10ᵏ uses counting.
def distinct_upto_pow10(k):
total, avail, prod = 1, 9, 9 # 1 counts the number 0
for d in range(1, k + 1):
total += prod
prod *= avail; avail -= 1
return total
print(distinct_upto_pow10(2), distinct_upto_pow10(3), sum(len(set(str(x))) == len(str(x)) for x in range(1000)))What is printed?- A. 90 738 739
- B. 91 739 739
- C. 91 648 739
- D. 100 739 739
Correct answer: B. 91 739 739
Numbers with d digits and no repeats: 9 · 9 · 8 · … (first digit 1–9, then the remaining choices). Below 100: 1 (zero) + 9 + 81 = 91. Below 1000: add 9·9·8 = 648 → 739, confirmed by brute force over 0..999. O(k) versus O(10ᵏ).
More Coding Interview Problems questions
- Problem: Find the minimum window length in which every one of k distinct product IDs appears at least once (k = number of distinct values…
- Problem: Find the number of operations to reduce n to 1: if even halve it, else add or subtract 1 (choose optimally). python def steps(n)…
- Problem: Find the minimum number of jumps for a knight to reach a target on an infinite board (BFS). python from collections import deque…
- Problem: Count the inversions of an array with merge sort. python def inv(a): if len(a) < 2: return a, 0 m = len(a) // 2 L, x =…
- Problem: Return the maximum sum of a subarray of size exactly k, and the sum of the best circular subarray (wrapping allowed). python def…
- Problem: Maintain prefix sums under point updates with a Fenwick tree (1-indexed). cpp int n = 8; vector<long long> t(n + 1, 0); void…