Remove Zero Digits and Multiply by Their Sum

Quick Overview

Remove zero digits in order and multiply the retained integer by its digit sum. Covers zero input, exact wide arithmetic, fresh examples, proof, and C++ code.

Remove Zero Digits and Multiply by Their Sum

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a nonnegative integer `n`, form an integer `x` by removing every zero digit from its ordinary decimal representation, while keeping the other digits in their original order. If no digit remains, use `x=0`. Let `digitSum` be the sum of the digits in `x`. Return the exact value `x * digitSum`. ```cpp class Solution { public: long long sumAndMultiply(int n); }; ``` ### Examples **Example 1:** ```text Input: n = 405060 Output: 6840 Explanation: Removing zeros leaves x = 456. Its digit sum is 4+5+6 = 15, so the result is 456*15 = 6840. ``` **Example 2:** ```text Input: n = 0 Output: 0 Explanation: No nonzero digits remain, so x and its digit sum are both zero. ``` ### Constraints - `0 <= n <= 1000000000`. ### Clarifying Questions - Can the retained digits be reordered? No. - Is the sum the sum of the retained digits, rather than their count? Yes. - Is the result reduced modulo anything? No; return the exact product. - Must the `long long` result type be preserved? Yes; the product can exceed a 32-bit signed integer. ### What a Strong Answer Covers - Removing zeros while preserving the complete order of all remaining digits. - The empty-retained-digit case and the exact product definition. - Internal zeros, repeated digits, and the largest input. - A correctness argument and safe numeric and resource bounds. ### Follow-up Questions - What would need to change in the interface if the input had arbitrarily many digits? - How would returning the retained representation change the output contract? ```hint Follow the decimal digits in order Scan the decimal representation from left to right. For each nonzero digit, append it numerically to x and add it to the digit sum. Leave both accumulators at zero when nothing is retained, then multiply using a wide integer type. ```

Overview: Remove zero digits in order and multiply the retained integer by its digit sum. Covers zero input, exact wide arithmetic, fresh examples, proof, and C++ code.

Microsoft logo
Microsoft
Apr 26, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a nonnegative integer n, form an integer x by removing every zero digit from its ordinary decimal representation, while keeping the other digits in their original order. If no digit remains, use x=0.

Let digitSum be the sum of the digits in x. Return the exact value x * digitSum.

class Solution {
public:
    long long sumAndMultiply(int n);
};

Examples

Example 1:

Input: n = 405060
Output: 6840
Explanation: Removing zeros leaves x = 456. Its digit sum is 4+5+6 = 15, so the result is 456*15 = 6840.

Example 2:

Input: n = 0
Output: 0
Explanation: No nonzero digits remain, so x and its digit sum are both zero.

Constraints

  • 0 <= n <= 1000000000 .

Clarifying Questions Guidance

  • Can the retained digits be reordered? No.
  • Is the sum the sum of the retained digits, rather than their count? Yes.
  • Is the result reduced modulo anything? No; return the exact product.
  • Must the long long result type be preserved? Yes; the product can exceed a 32-bit signed integer.

What a Strong Answer Covers Guidance

  • Removing zeros while preserving the complete order of all remaining digits.
  • The empty-retained-digit case and the exact product definition.
  • Internal zeros, repeated digits, and the largest input.
  • A correctness argument and safe numeric and resource bounds.

Follow-up Questions Guidance

  • What would need to change in the interface if the input had arbitrarily many digits?
  • How would returning the retained representation change the output contract?