3345. Smallest Divisible Digit Product I
Asked at Microsoft
Difficulty
Easy
Time
O(k * d)
Space
O(1)
Pattern
Math
Companies
1
Problem
You are given two integers, n and t. Find the smallest integer x such that x >= n and the product of the digits of x is divisible by t.
To get the digit product, multiply every digit of the number together. For example, the digit product of 16 is 1 * 6 = 6. A number is a valid answer when that product can be divided by t with no remainder.
If several numbers work, return the smallest one. Note that any number containing the digit 0 has a digit product of 0, and 0 is divisible by every t, so such numbers always qualify.
Check numbers from n upward
Constraints
- ●
1 <= n <= 100 - ●
1 <= t <= 10
Examples
n = 10, t = 210n = 15, t = 316Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Microsoft | Easy | View all Microsoft questions → |
How to Think About It
Brute force: start from 1 and check each integer until you find one whose digit product is divisible by n.
Enumerate all single-digit numbers first; if any digit is divisible by n, return it.
For two-digit numbers, try all combinations where the product of digits is divisible by n.
Use early termination: if n is 1, return 1 immediately.
Optimal: enumerate candidates in increasing order, computing digit product on the fly, and return the first match.
Optimal Approach
Iterate through all positive integers starting from 1. For each candidate, compute the product of its digits. If the product is divisible by n, return the candidate. Since n is small (typically <= 100), the answer is guaranteed to be found quickly. Single-digit numbers should be checked first, then two-digit numbers, and so on.
Try each number and multiply its digits
What Trips People Up in Real Interviews
Clarify constraints on n — typically small values make brute force acceptable.
Remember that single-digit numbers (1-9) should be checked before multi-digit.
If n itself is a single digit, the answer might be n itself.
Edge case: n = 1, the answer is 1 since any number with product 1 works.
Do not overcomplicate — simple enumeration with digit product calculation is sufficient.
Solution Code
O(k * d) timedef smallestNumber(n):
for num in range(1, 100000):
product = 1
temp = num
while temp > 0:
product *= temp % 10
temp //= 10
if product % n == 0:
return numFrequently Asked Questions
What is the Smallest Divisible Digit Product I problem?
Given an integer n, find the smallest positive integer such that the product of its digits is divisible by n. Return this integer.
How do you solve Smallest Divisible Digit Product I?
The optimal approach runs in O(k * d) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Smallest Divisible Digit Product I?
Smallest Divisible Digit Product I is asked at Microsoft. It is a easy difficulty problem.
What are common mistakes on Smallest Divisible Digit Product I?
- Clarify constraints on n — typically small values make brute force acceptable.
- Remember that single-digit numbers (1-9) should be checked before multi-digit.
- If n itself is a single digit, the answer might be n itself.
- Edge case: n = 1, the answer is 1 since any number with product 1 works.
- Do not overcomplicate — simple enumeration with digit product calculation is sufficient.