Magicsheet logo

Smallest Divisible Digit Product II

Hard
87.5%
Updated 8/1/2025

Smallest Divisible Digit Product II

What is this problem about?

Building on the previous concept, "Smallest Divisible Digit Product II" is a "HARD" level version of the problem. You are given a large number (represented as a string) and a target t. You need to find the smallest number represented as a string that is greater than or equal to the input and whose digit product is divisible by t. This "Smallest Divisible Digit Product II coding problem" is significantly harder because the numbers can be extremely long (hundreds of digits), making brute-force enumeration impossible.

Why is this asked in interviews?

Microsoft and other top firms ask this to test advanced algorithmic thinking, specifically backtracking and greedy strategies. It requires you to consider prime factorization—since the product of digits must be divisible by t, the digits themselves must provide all the prime factors of t (only 2, 3, 5, and 7, since digits are 0-9). This problem evaluates your ability to handle large integers and perform sophisticated state-space searches.

Algorithmic pattern used

This problem follows the "Greedy, Backtracking, and Number Theory interview pattern".

  1. Factorize t. If t has prime factors other than 2, 3, 5, or 7, it's impossible for any digit product to be divisible by t unless a digit is 0.
  2. Try to match the input string digit by digit.
  3. Use backtracking to find the first position where you can change a digit to something larger and still fulfill the remaining prime factor requirements with the following digits.
  4. To keep the number "smallest," once you change a digit at position i, you greedily fill the remaining positions with the smallest digits possible (usually starting with '1's and ending with the necessary factors).

Example explanation

Suppose the input is "123" and t = 24.

  1. 24 = 2^3 * 3.
  2. Can we use "123"? Product = 6. 24 / 6 = 4. We need more factors.
  3. Try changing the last digit. "124" -> 8. "126" -> 12. "128" -> 16.
  4. Try changing the second digit. "13...". If we use "138", product is 24. 24 is divisible by 24. This requires a careful search of possible digit combinations to find the minimal string.

Common mistakes candidates make

The biggest mistake is attempting a brute-force numerical approach, which will fail for strings of length 100. Another error is not realizing that only the prime factors 2, 3, 5, and 7 matter. Candidates also struggle with the backtracking logic, particularly correctly "resetting" the state when a path fails or ensuring that the resulting string is the absolute smallest possible.

Interview preparation tip

When a math problem involves products of digits, always think about prime factors. Every digit from 1-9 can be broken down into factors of 2, 3, 5, and 7. This "Smallest Divisible Digit Product II interview question" is a masterclass in combining number theory with search algorithms. Practice backtracking on strings to get comfortable with this pattern.

Similar Questions