KatInterview

625. Minimum Factorization

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the smallest positive integer x whose digits, when multiplied together, equal a given positive integer num. If no such x exists or if x exceeds the 32-bit signed integer limit, we should return 0.

Solution

Minimum Factorization in Python

class Solution:
    def smallestFactorization(self, num: int) -> int:
        # Base case: if num is a single digit, it's its own smallest factorization.
        if num < 10:
            return num

        # Store the digits found in a list.
        digits = []
        
        # Greedily find factors from 9 down to 2.
        # This ensures we use the largest factors first, minimizing the number of digits.
        for d in range(9, 1, -1):
            while num % d == 0:
                num //= d
                digits.append(str(d))

        # If num is still greater than 1, it means it has a prime factor > 9.
        if num > 1:
            return 0

        # The digits are found in decreasing order (e.g., [8, 6] for num=48).
        # To form the smallest number, they must be sorted in increasing order.
        # Reversing the list achieves this (e.g., [6, 8]).
        # Then join to form the string "68" and convert to an integer.
        result_str = "".join(reversed(digits))
        result = int(result_str)

        # Check if the result fits in a 32-bit signed integer.
        if result > 2**31 - 1:
            return 0
        
        return result

Minimum Factorization in Java

class Solution {
    public int smallestFactorization(int num) {
        // Base case: if num is a single digit, it's its own smallest factorization.
        if (num < 10) {
            return num;
        }

        // Use long to prevent overflow during construction of the result.
        long ans = 0;
        long power_of_10 = 1;

        // Greedily find factors from 9 down to 2.
        // This ensures we use the largest factors first, minimizing the number of digits.
        for (int d = 9; d >= 2; d--) {
            while (num % d == 0) {
                num /= d;
                // Prepend the digit `d` to the result.
                // This places larger digits at less significant positions, forming the smallest number.
                ans = d * power_of_10 + ans;
                power_of_10 *= 10;
                
                // Early overflow check for the constructed number.
                if (ans > Integer.MAX_VALUE) {
                    return 0;
                }
            }
        }

        // If num is still greater than 1, it means it has a prime factor > 9.
        if (num > 1) {
            return 0;
        }

        // The final result is already checked for overflow inside the loop.
        return (int)ans;
    }
}

Minimum Factorization in C++

class Solution {
public:
    int smallestFactorization(int num) {
        // Base case: if num is a single digit, it's its own smallest factorization.
        if (num < 10) {
            return num;
        }

        // Use long long to prevent overflow during construction of the result.
        long long ans = 0;
        long long power_of_10 = 1;

        // Greedily find factors from 9 down to 2.
        // This ensures we use the largest factors first, minimizing the number of digits.
        for (int d = 9; d >= 2; --d) {
            while (num % d == 0) {
                num /= d;
                // Prepend the digit `d` to the result.
                // This places larger digits at less significant positions, forming the smallest number.
                ans = d * power_of_10 + ans;
                power_of_10 *= 10;

                // Early overflow check for the constructed number.
                if (ans > INT_MAX) {
                    return 0;
                }
            }
        }

        // If num is still greater than 1, it means it has a prime factor > 9.
        if (num > 1) {
            return 0;
        }

        // Final check for overflow (although the check inside the loop often catches it).
        if (ans > INT_MAX) {
            return 0;
        }

        return (int)ans;
    }
};

Minimum Factorization in TypeScript

function smallestFactorization(num: number): number {
    // Base case: if num is a single digit, it's its own smallest factorization.
    if (num < 10) {
        return num;
    }

    // JavaScript's `number` type is a 64-bit float, which can safely handle integers
    // up to 2^53 - 1. This is sufficient to avoid overflow during construction.
    let ans = 0;
    let power_of_10 = 1;
    const INT_MAX = 2**31 - 1;

    // Greedily find factors from 9 down to 2.
    // This ensures we use the largest factors first, minimizing the number of digits.
    for (let d = 9; d >= 2; d--) {
        while (num % d === 0) {
            num /= d;
            // Prepend the digit `d` to the result.
            // This places larger digits at less significant positions, forming the smallest number.
            ans = d * power_of_10 + ans;
            power_of_10 *= 10;
            
            // Early overflow check for the constructed number.
            if (ans > INT_MAX) {
                return 0;
            }
        }
    }

    // If num is still greater than 1, it means it has a prime factor > 9.
    if (num > 1) {
        return 0;
    }

    // The final result is already checked for overflow inside the loop.
    return ans;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.