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
Constraints
- ●
1 <= s.length <= 100 - ●
2 <= k <= 100 - ●
s consists of digits only
Examples
s = "11111222223", k = 3"135"s = "00000000", k = 3"000"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Capital One | Easy | View all Capital One questions → |
How to Think About It
Loop while len(s) > k.
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).
The last chunk can be shorter than k — slicing handles that naturally.
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.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) per round, and the number o timedef 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 sFrequently 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.