Return the product of an integer's digits minus their sum

Quick Overview

Compute the product of a positive integer's decimal digits minus the sum of those digits. The problem tests digit extraction with modulo and integer division, handling zero digits, and stating time and space cost in terms of the number of digits.

Return the product of an integer's digits minus their sum

Company: Postmates

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a positive integer `n`, return the product of its digits minus the sum of its digits. ### Function Signature ```python def subtract_product_and_sum(n: int) -> int: ``` ### Rules - The digits are the base-10 digits of `n`. - Return `product - sum` as an integer. The result can be zero or negative. ### Constraints - `1 <= n <= 10^5` ### Examples ```text Input: n = 234 Output: 15 ``` The digits are 2, 3 and 4. Their product is 24 and their sum is 9, and `24 - 9 = 15`. ```text Input: n = 4421 Output: 21 ``` The product is `4 * 4 * 2 * 1 = 32` and the sum is `4 + 4 + 2 + 1 = 11`, and `32 - 11 = 21`. ```text Input: n = 100000 Output: -1 ``` A single zero digit makes the product 0. The sum is 1, so the answer is `0 - 1 = -1`.

Overview: Compute the product of a positive integer's decimal digits minus the sum of those digits. The problem tests digit extraction with modulo and integer division, handling zero digits, and stating time and space cost in terms of the number of digits.

Postmates logo
Postmates
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a positive integer n, return the product of its digits minus the sum of its digits.

Function Signature

def subtract_product_and_sum(n: int) -> int:

Rules

  • The digits are the base-10 digits of n .
  • Return product - sum as an integer. The result can be zero or negative.

Constraints

  • 1 <= n <= 10^5

Examples

Input: n = 234
Output: 15

The digits are 2, 3 and 4. Their product is 24 and their sum is 9, and 24 - 9 = 15.

Input: n = 4421
Output: 21

The product is 4 * 4 * 2 * 1 = 32 and the sum is 4 + 4 + 2 + 1 = 11, and 32 - 11 = 21.

Input: n = 100000
Output: -1

A single zero digit makes the product 0. The sum is 1, so the answer is 0 - 1 = -1.