Quick Overview

Given n and k, find the k-th integer in the range 1 to n when the numbers are ordered as decimal strings in dictionary order. Since n can be as large as one billion, it tests reasoning about the structure of that order and counting numbers without listing them.

Find the k-th smallest integer from 1 to n in lexicographical order

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given two integers `n` and `k`, return the `k`-th lexicographically smallest integer in the range `[1, n]`. ### Function Signature ```python def find_kth_number(n: int, k: int) -> int: ``` ### Rules - Lexicographic order compares the decimal representations as strings, with no leading zeros. For example, `10` comes before `2`, and `1` comes before `10`. - `k` is 1-indexed: `k = 1` asks for the first number in that order. ### Constraints - `1 <= n <= 10^9` - `1 <= k <= n` ### Examples ```text Input: n = 13, k = 2 Output: 10 ``` The lexicographic order of 1 to 13 is `[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]`, and the 2nd element is 10. ```text Input: n = 1, k = 1 Output: 1 ``` Only one number exists in the range, so the answer is 1. ```text Input: n = 100, k = 15 Output: 21 ``` The numbers that start with the digit 1 come first: 1, 10, 100, 11 to 19, which is 12 numbers. They are followed by 2, 20 and 21, so the 15th number is 21.

Overview: Given n and k, find the k-th integer in the range 1 to n when the numbers are ordered as decimal strings in dictionary order. Since n can be as large as one billion, it tests reasoning about the structure of that order and counting numbers without listing them.

Given two integers `n` and `k`, return the `k`-th lexicographically smallest integer in the range `[1, n]`. ### Rules - Lexicographic order compares the decimal representations as strings, with no leading zeros. For example, `10` comes before `2`, and `1` comes before `10`. - `k` is 1-indexed: `k = 1` asks for the first number in that order. ### Function Signature ```python def find_kth_number(n, k): ``` Return a single integer: the `k`-th number when `1, 2, ..., n` are listed in lexicographic order. ### Examples **Example 1** ```text Input: n = 13, k = 2 Output: 10 ``` The lexicographic order of 1 to 13 is `[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]`, and the 2nd element is 10. **Example 2** ```text Input: n = 100, k = 15 Output: 21 ``` The numbers that start with the digit 1 come first: 1, 10, 100, 11 to 19, which is 12 numbers. They are followed by 2, 20 and 21, so the 15th number is 21. ### Constraints - `1 <= n <= 10^9` - `1 <= k <= n` Both inputs and the answer are at most 10^9, so they fit in a signed 32-bit integer. Values computed along the way can exceed 2^31 - 1 when `n` is near 10^9, so use 64-bit arithmetic (`long` in Java, `long long` in C++) for them.

Constraints

  • 1 <= n <= 10^9
  • 1 <= k <= n

Examples

Input: (1, 1)

Expected Output: 1

Explanation: Minimum valid input: the range holds only 1.

Input: (13, 2)

Expected Output: 10

Explanation: Source example: the order is 1, 10, 11, 12, 13, 2, ..., 9, so the 2nd number is 10.

Hints

  1. Writing out all n numbers and sorting them as strings is correct for small n, but n can be as large as 10^9.
  2. In the second example, the 12 numbers that start with the digit 1 all come before 2. Knowing how many numbers in [1, n] begin with a given digit string tells you how many positions that whole group takes up.
  3. Check boundary cases by hand: n equal to a power of ten such as 10 or 100, and k equal to n.

K-th Number in Lexicographic Order from 1 to n

hardByteDanceSoftware Engineer

Given two integers n and k, return the k-th lexicographically smallest integer in the range [1, n].

Rules

  • Lexicographic order compares the decimal representations as strings, with no leading zeros. For example, 10 comes before 2, and 1 comes before 10.
  • k is 1-indexed: k = 1 asks for the first number in that order.

Function Signature

def find_kth_number(n, k):

Return a single integer: the k-th number when 1, 2, ..., n are listed in lexicographic order.

Examples

Example 1

Input: n = 13, k = 2
Output: 10

The lexicographic order of 1 to 13 is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], and the 2nd element is 10.

Example 2

Input: n = 100, k = 15
Output: 21

The numbers that start with the digit 1 come first: 1, 10, 100, 11 to 19, which is 12 numbers. They are followed by 2, 20 and 21, so the 15th number is 21.

Constraints

  • 1 <= n <= 10^9
  • 1 <= k <= n

Both inputs and the answer are at most 10^9, so they fit in a signed 32-bit integer. Values computed along the way can exceed 2^31 - 1 when n is near 10^9, so use 64-bit arithmetic (long in Java, long long in C++) for them.

Examples

Example 1
Input
(1, 1)
Output
1
Notes
Minimum valid input: the range holds only 1.
Example 2
Input
(13, 2)
Output
10
Notes
Source example: the order is 1, 10, 11, 12, 13, 2, ..., 9, so the 2nd number is 10.

Constraints

  • •1 <= n <= 10^9
  • •1 <= k <= n

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def find_kth_number(n: int, k: int) -> int:
Sample call
find_kth_number(1, 1)
Language
Case 1
Input
(1, 1)
Expected
1
Case 2
Input
(13, 2)
Expected
10

Browse More Questions

Show the approach

Approach

Think of the numbers 1..n as a prefix tree. The roots are the digits 1 to 9, and the children of a number x are 10x, 10x + 1, ..., 10x + 9, keeping only those that are at most n. Listing this tree in preorder (a node first, then each child subtree from digit 0 to digit 9) gives exactly the lexicographic order, because a digit string comes before all of its extensions and two siblings compare by their last digit.

The algorithm walks that preorder without building it. It keeps the current number cur (starting at 1) and remaining = k - 1, the number of positions still to move forward. To size the subtree of cur it scans one depth at a time: at depth d the numbers with prefix cur form the interval [first, last] = [cur * 10^d, cur * 10^d + 10^d - 1], and while first <= n the part inside the range adds min(n, last) - first + 1. If that subtree size is at most remaining, every number in the subtree comes before the target, so the walk skips it (remaining -= size, cur += 1). Otherwise the target is inside the subtree but is not cur itself, so the walk steps onto the first child (remaining -= 1, cur *= 10). When remaining reaches 0, cur is the answer.

Invariant: the k-th number is exactly remaining positions after cur in preorder (0 means cur itself), and it lies in the subtree of cur or of a later sibling of cur under the same parent. Skipping keeps this true because the skipped subtree is exactly the block of positions between cur and cur + 1. Descending keeps it true because the target lies strictly inside the subtree of cur. The walk never moves past a last sibling (digit 9), since it only descended into a parent whose subtree had more nodes than remaining.

Edge cases: k = 1 (including n = 1) returns 1 without any counting. When n is a power of ten such as 10, 100 or 1000, the group that starts with 1 holds one extra number (n itself). Groups whose deepest level is cut off by n, such as 5000 to 5432 when n = 5432, are counted with the min(n, last) clamp. For n near 10^9 the interval bounds reach about 2 * 10^10, so Java and C++ use long and long long for the intermediate values even though the inputs and the answer fit in int.

Time complexity:
O((log n)^2)
Space complexity:
O(1)