EasyStringSimulationUpdated Sep 2026

2243. Calculate Digit Sum of a String

Asked at Capital One

Difficulty

Easy

Time

O(n) per round, and the number o

Space

O(n)

Pattern

String

Companies

1

Problem

You are given a string s made only of digits, and an integer k.

While the length of s is greater than k, do one round: cut s into consecutive chunks of k characters (the last chunk may be shorter), replace each chunk with the sum of its digits written as a string, then join those pieces back together to form the new s.

Stop when len(s) <= k and return the final string. Note that a chunk's digit sum can be more than one digit, so the string does not always shrink by exactly the factor you expect.

Split, sum each chunk, join

s1111122222311111222223012345678910k = 3, so we cut into chunks of three, and the last chunk may be shorternew s34650123Each chunk becomes its digit sum: 1+1+1=3, 1+1+2=4, 2+2+2=6, 2+3=5
One round turns every group of k digits into a single number, then glues the results together.

Constraints

  • ●1 <= s.length <= 100
  • ●2 <= k <= 100
  • ●s consists of digits only

Examples

Example 1
Inputs = "11111222223", k = 3
Output"135"
WhyRound 1 cuts into "111", "112", "222", "23". Their digit sums are 3, 4, 6, 5, so s becomes "3465". Round 2 cuts into "346" and "5", giving 13 and 5, so s becomes "135". Its length is 3, which is not greater than k, so we stop.
Example 2
Inputs = "00000000", k = 3
Output"000"
WhyRound 1 cuts into "000", "000", "00". Every digit sum is 0, so s becomes "000". Its length equals k, so the loop ends and we return "000".

Asked At

1 companies
CompanyDifficulty
Capital OneEasyView all Capital One questions →

How to Think About It

1

Loop while len(s) > k.

2

Each round: walk the string in steps of k, sum the digits of each chunk, and append that sum as a string (it may have several digits).

3

The last chunk can be shorter than k — slicing handles that naturally.

4

Each round shrinks the string substantially (a chunk of k digits becomes at most ceil(log10(9k + 1)) characters), so only a few rounds happen.

5

Walkthrough: "11111222223", k = 3: 111|112|222|23 -> 3,4,6,5 -> "3465"; then 346|5 -> 13,5 -> "135". Length 3 -> done.

Optimal Approach

Step 1: While len(s) > k:
parts = []
For i in 0, k, 2k, ...: parts.append(str(sum of digits in s[i:i+k])).
s = "".join(parts).
Step 2: Return s.

Time: O(n) per round, and the length shrinks geometrically. Space: O(n).

Repeat until the string is short enough

len(s) > k ?cut into chunks of ksum digits of each chunkjoin the sums into new sThe loop body: one full round of the simulationround 1346534650123Length 4 > k = 3, so we run another round on "3465"round 21350123+4+6=13 and 5=5, giving "135"; length 3 is notgreater than k, so we stopwhile len(s) > k: s = "".join(str(sum of digits in s[i:i+k]))
Each round shrinks the string, so only a few rounds are needed before the length is at most k.
ComplexityTime O(n) per round, and the number oSpace O(n)Each round scans the whole string once and builds a new one; the total work is dominated by the first round.

What Trips People Up in Real Interviews

1

Stopping when the length is less than k instead of at most k.

2

Summing the group into a single digit — the sum is written in full (for example 13).

3

Dropping the final short group.

4

Converting the whole string to an integer, which overflows for long inputs.

Solution Code

O(n) per round, and the number o time
def digitSum(s, k):
    while len(s) > k:
        parts = []
        for i in range(0, len(s), k):
            parts.append(str(sum(int(ch) for ch in s[i:i + k])))
        s = ''.join(parts)
    return s

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Calculate Digit Sum of a String problem?

Calculate Digit Sum of a String repeatedly splits a digit string into groups of `k` characters, replaces each group with the sum of its digits (written as a string), and joins them — until the string has length at most `k`. It is a direct simulation that tests careful string handling.

How do you solve Calculate Digit Sum of a String?

The optimal approach runs in O(n) per round, and the number o time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Calculate Digit Sum of a String?

Calculate Digit Sum of a String is asked at Capital One. It is a easy difficulty problem.

What are common mistakes on Calculate Digit Sum of a String?
  • Stopping when the length is less than `k` instead of at most `k`.
  • Summing the group into a single digit — the sum is written in full (for example `13`).
  • Dropping the final short group.
  • Converting the whole string to an integer, which overflows for long inputs.