Find the Next Number with a Divisible Digit Product

Quick Overview

Find the smallest integer at least n whose decimal digit product is divisible by t. Zero digits are allowed. Includes worked examples and a C++ answer.

Find the Next Number with a Divisible Digit Product

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Find the Next Number with a Divisible Digit Product ### Problem Given positive integers `n` and `t`, return the smallest integer greater than or equal to `n` whose decimal digit product is divisible by `t`. Zero digits are allowed. If any digit is zero, the digit product is zero, and zero is divisible by every positive `t`. ### C++ Interface ```cpp class Solution { public: int smallestNumber(int n, int t); }; ``` ### Examples #### Example 1 **Input:** `n = 26, t = 7` **Output:** `27` **Explanation:** The product for `26` is `12`, which is not divisible by `7`. The next number, `27`, has product `14`, which is divisible by `7`. #### Example 2 **Input:** `n = 38, t = 5` **Output:** `40` **Explanation:** The products for `38` and `39` are `24` and `27`. Neither is divisible by `5`. The product for `40` is zero, so `40` qualifies. ### Constraints - `1 <= n <= 100` - `1 <= t <= 10` ### Clarifying Questions - Is `n` itself a candidate? Yes. - Are zero digits forbidden? No, they are allowed in this problem. - Do we use the digit sum? No, multiply the decimal digits. ### What a Strong Answer Covers - Returns the smallest qualifying number, including `n` when it already qualifies. - Computes products correctly when a decimal digit is zero. - Establishes that a qualifying candidate will be found and gives bounds under the stated limits. ### Follow-up Questions - What happens when `t = 1`? - Why is a qualifying number always nearby? ```hint Look at decimal boundaries Every multiple of ten ends in zero. How far away can the next such number be? ```

Overview: Find the smallest integer at least n whose decimal digit product is divisible by t. Zero digits are allowed. Includes worked examples and a C++ answer.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Find the Next Number with a Divisible Digit Product

Problem

Given positive integers n and t, return the smallest integer greater than or equal to n whose decimal digit product is divisible by t.

Zero digits are allowed. If any digit is zero, the digit product is zero, and zero is divisible by every positive t.

C++ Interface

class Solution {
public:
    int smallestNumber(int n, int t);
};

Examples

Example 1

Input: n = 26, t = 7

Output: 27

Explanation: The product for 26 is 12, which is not divisible by 7. The next number, 27, has product 14, which is divisible by 7.

Example 2

Input: n = 38, t = 5

Output: 40

Explanation: The products for 38 and 39 are 24 and 27. Neither is divisible by 5. The product for 40 is zero, so 40 qualifies.

Constraints

  • 1 <= n <= 100
  • 1 <= t <= 10

Clarifying Questions Guidance

  • Is n itself a candidate? Yes.
  • Are zero digits forbidden? No, they are allowed in this problem.
  • Do we use the digit sum? No, multiply the decimal digits.

What a Strong Answer Covers Guidance

  • Returns the smallest qualifying number, including n when it already qualifies.
  • Computes products correctly when a decimal digit is zero.
  • Establishes that a qualifying candidate will be found and gives bounds under the stated limits.

Follow-up Questions Guidance

  • What happens when t = 1 ?
  • Why is a qualifying number always nearby?