The "Smallest Number With Given Digit Product" interview question asks you to find the smallest positive integer whose digits, when multiplied together, equal a given value n. For example, if n = 12, the smallest number is 26 because 2 * 6 = 12. While 34 or 43 also have a product of 12, 26 is the smallest. This "Smallest Number With Given Digit Product coding problem" requires working backwards from the product to the digits.
Microsoft and other top companies ask this to evaluate a candidate's greedy algorithmic thinking and basic number theory. It tests if you can recognize that to keep the number "smallest," you should use as few digits as possible (which means using larger digits like 8 and 9) and arrange them in increasing order. It also tests your ability to handle cases where no such number exists (e.g., if n has a prime factor greater than 7).
This problem follows the "Math and Greedy interview pattern".
n < 10, the smallest number is simply n.n >= 10, try to divide n by the largest possible single-digit divisors starting from 9 down to 2.n is divisible by a digit d, add d to a list and update n = n / d.n is still greater than 1, it means n had a prime factor like 11, 13, etc. No such number exists; return -1.Let n = 36.
36 % 9 == 0. Digits: [9]. n = 4.4 % 4 == 0. Digits: [9, 4]. n = 1.[4, 9].
Result: 49.
(Note: 66 also works, but 49 is smaller).A common error is trying to find the digits using a smaller-to-larger approach (like 2, 3...). This results in more digits (e.g., for 12, it might give 223 instead of 26), making the number much larger. Another mistake is forgetting to handle the case where n cannot be factorized into single digits. Some candidates also struggle with n=0 or n=1, which are important edge cases.
In "smallest number" problems involving digits, "Greedy" usually means "maximize the value of the trailing digits to minimize the total number of digits." This "Smallest Number With Given Digit Product interview question" perfectly illustrates the power of greedy factorization.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Broken Calculator | Medium | Solve | |
| Maximum Swap | Medium | Solve | |
| Determine the Minimum Sum of a k-avoiding Array | Medium | Solve | |
| Find the Minimum Number of Fibonacci Numbers Whose Sum Is K | Medium | Solve | |
| Find the Minimum Possible Sum of a Beautiful Array | Medium | Solve |