Reduce Consecutive Digit Groups until the String Is Short Enough

Quick Overview

Repeatedly replace consecutive digit groups with their decimal digit sums. Return the final string, preserving zero groups and the shorter last group.

Reduce Consecutive Digit Groups until the String Is Short Enough

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem You are given a digit string `s` and an integer `k`. While `s` has more than `k` characters, perform a round: 1. Split `s` from left to right into consecutive groups of `k` characters. The last group may be shorter. 2. Replace each group with the decimal representation of the sum of its digits. 3. Concatenate the replacements in group order. Return the string when its length is at most `k`. ```cpp class Solution { public: string digitSum(string s, int k); }; ``` ### Examples **Example 1:** ```text Input: s = "987654", k = 3 Output: "75" Explanation: The first round produces "2415" from sums 24 and 15. The next round produces "75" from sums 7 and 5. ``` **Example 2:** ```text Input: s = "1234", k = 4 Output: "1234" Explanation: The length already equals k, so no round occurs. ``` ### Constraints - `1 <= s.length <= 100`. - `2 <= k <= 100`. - s contains decimal digits only. ### Clarifying Questions - Is a group sum reduced to a single digit immediately? No. Write its full decimal representation; another round may process those digits later. - How is a zero sum written? As one `0` character for that group. - What if the initial length is already at most `k`? Return `s` unchanged. ```hint Check the boundary Keep the replacement of a group separate from any later round. A sum such as 18 contributes two characters in the current round. ```

Overview: Repeatedly replace consecutive digit groups with their decimal digit sums. Return the final string, preserving zero groups and the shorter last group.

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

Problem

You are given a digit string s and an integer k. While s has more than k characters, perform a round:

  1. Split s from left to right into consecutive groups of k characters. The last group may be shorter.
  2. Replace each group with the decimal representation of the sum of its digits.
  3. Concatenate the replacements in group order.

Return the string when its length is at most k.

class Solution {
public:
    string digitSum(string s, int k);
};

Examples

Example 1:

Input: s = "987654", k = 3
Output: "75"
Explanation: The first round produces "2415" from sums 24 and 15. The next round produces "75" from sums 7 and 5.

Example 2:

Input: s = "1234", k = 4
Output: "1234"
Explanation: The length already equals k, so no round occurs.

Constraints

  • 1 <= s.length <= 100 .
  • 2 <= k <= 100 .
  • s contains decimal digits only.

Clarifying Questions Guidance

  • Is a group sum reduced to a single digit immediately? No. Write its full decimal representation; another round may process those digits later.
  • How is a zero sum written? As one 0 character for that group.
  • What if the initial length is already at most k ? Return s unchanged.