Find the nth smallest number that uses only a given set of digits

Quick Overview

Find the nth smallest positive number that can be written using only a given sorted set of digits, and return it as a string. It tests counting numbers by length and converting a rank inside a length group into bijective base-k digits.

Find the nth smallest number that uses only a given set of digits

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a sorted array of unique digits `digits` and a positive integer `n`. Consider a custom number system where only the given digits can be used to form numbers. Find the `n`-th smallest number in this system and return it as a string. Numbers in this system are formed using only the provided digits, similar to how numbers in base 10 use digits 0-9, but here only the specified digits are allowed at each position. For example, if `digits = [1, 2, 3]`, the sequence of numbers is `1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33, 111, 112, ...` ### Function Signature ```python def find_nth_number(digits: list[int], n: int) -> str: ``` ### Rules - A number may use any digit from `digits` any number of times, at any length of 1 or more digits. No other digits are allowed. - The sequence is sorted by numeric value and `n` is 1-indexed, so `n = 1` is `digits[0]`. - Return the decimal digits as a string. The answer can be longer than a 64-bit integer: with `digits = [1, 2]` and `n = 10^8` it has 26 digits, and with a single digit the `n`-th number has `n` digits. ### Constraints - `1 <= digits.length <= 9` - `1 <= digits[i] <= 9` - All elements in `digits` are unique and sorted in ascending order. - `1 <= n <= 10^8` ### Examples ```text Input: digits = [1, 2, 3], n = 10 Output: "31" ``` The sequence is 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, so the 10th number is 31. ```text Input: digits = [2, 4, 6, 8], n = 5 Output: "22" ``` The four one-digit numbers 2, 4, 6, 8 come first, and the smallest two-digit number, 22, is 5th. ```text Input: digits = [7], n = 3 Output: "777" ``` With one digit there is exactly one number of each length: 7, 77, 777. The 3rd is 777.

Overview: Find the nth smallest positive number that can be written using only a given sorted set of digits, and return it as a string. It tests counting numbers by length and converting a rank inside a length group into bijective base-k digits.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a sorted array of unique digits digits and a positive integer n. Consider a custom number system where only the given digits can be used to form numbers.

Find the n-th smallest number in this system and return it as a string.

Numbers in this system are formed using only the provided digits, similar to how numbers in base 10 use digits 0-9, but here only the specified digits are allowed at each position. For example, if digits = [1, 2, 3], the sequence of numbers is 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33, 111, 112, ...

Function Signature

def find_nth_number(digits: list[int], n: int) -> str:

Rules

  • A number may use any digit from digits any number of times, at any length of 1 or more digits. No other digits are allowed.
  • The sequence is sorted by numeric value and n is 1-indexed, so n = 1 is digits[0] .
  • Return the decimal digits as a string. The answer can be longer than a 64-bit integer: with digits = [1, 2] and n = 10^8 it has 26 digits, and with a single digit the n -th number has n digits.

Constraints

  • 1 <= digits.length <= 9
  • 1 <= digits[i] <= 9
  • All elements in digits are unique and sorted in ascending order.
  • 1 <= n <= 10^8

Examples

Input: digits = [1, 2, 3], n = 10
Output: "31"

The sequence is 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, so the 10th number is 31.

Input: digits = [2, 4, 6, 8], n = 5
Output: "22"

The four one-digit numbers 2, 4, 6, 8 come first, and the smallest two-digit number, 22, is 5th.

Input: digits = [7], n = 3
Output: "777"

With one digit there is exactly one number of each length: 7, 77, 777. The 3rd is 777.