Find the smallest number whose digits multiply to a given integer

Quick Overview

Given a positive integer, find the smallest positive integer whose digits multiply to exactly that value, or return 0 when none exists or it does not fit in a 32-bit signed integer. Tests reasoning about digit factorizations, place value and overflow checks.

Find the smallest number whose digits multiply to a given integer

Company: Tencent

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a positive integer `a`, find the smallest positive integer whose digits multiply to `a`. If there is no answer, or the answer does not fit in the range of a 32-bit signed integer, return `0`. ### Function Signature ```python def smallest_factorization(a: int) -> int: ``` ### Rules - The product of the digits of the returned number must equal `a` exactly. - Return `0` when no positive integer has digit product `a`, or when the smallest one is greater than `2147483647` (that is, `2^31 - 1`). - For `a = 1` the answer is `1`. ### Constraints - `1 <= a <= 2 * 10^9` ### Examples ```text Input: a = 48 Output: 68 ``` 48 = 6 * 8, so the smallest number whose digits multiply to 48 is 68. No one-digit number works, and no two-digit number below 68 has digit product 48. ```text Input: a = 15 Output: 35 ``` 15 = 3 * 5, so the smallest number whose digits multiply to 15 is 35. ```text Input: a = 774840978 Output: 0 ``` 774840978 is `2 * 9^9`, which needs at least ten digits, and the smallest number with that digit product is 2999999999. That is larger than 2147483647, so the answer is 0.

Overview: Given a positive integer, find the smallest positive integer whose digits multiply to exactly that value, or return 0 when none exists or it does not fit in a 32-bit signed integer. Tests reasoning about digit factorizations, place value and overflow checks.

Tencent logo
Tencent
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a positive integer a, find the smallest positive integer whose digits multiply to a.

If there is no answer, or the answer does not fit in the range of a 32-bit signed integer, return 0.

Function Signature

def smallest_factorization(a: int) -> int:

Rules

  • The product of the digits of the returned number must equal a exactly.
  • Return 0 when no positive integer has digit product a , or when the smallest one is greater than 2147483647 (that is, 2^31 - 1 ).
  • For a = 1 the answer is 1 .

Constraints

  • 1 <= a <= 2 * 10^9

Examples

Input: a = 48
Output: 68

48 = 6 * 8, so the smallest number whose digits multiply to 48 is 68. No one-digit number works, and no two-digit number below 68 has digit product 48.

Input: a = 15
Output: 35

15 = 3 * 5, so the smallest number whose digits multiply to 15 is 35.

Input: a = 774840978
Output: 0

774840978 is 2 * 9^9, which needs at least ten digits, and the smallest number with that digit product is 2999999999. That is larger than 2147483647, so the answer is 0.