Parse and Sum Integers
Easy About 10 min Free, no account needed
Sum space-separated integers (negatives included) allocation-free.
The brief
Given a string of integers separated by single spaces, negatives included,
like "12 -7 30", return their sum.
Split + int.Parse allocates a string per number. The hidden test has
15,000 numbers under a 32 KB allocation budget, so splitting the input blows
the budget. You will need to read the numbers straight from the string (mind the
optional - and the negatives). Watch your allocations.
Starter code
using System;
public class Solution
{
// TODO: return the sum of space-separated integers (with optional leading minus signs).
public long SumNumbers(string text)
{
throw new NotImplementedException();
}
}How it is graded
Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.
Related guides
What it practices
- String processing
- Parse, scan and match text character by character with correct edge cases.
- Allocation-free code
- Avoid per-item strings and copies with spans, indexes and arithmetic in hot loops.
Related katas
- Count Words Easy Free Count words by spotting boundaries, not by splitting.
- Count Substring Occurrences Medium Free Count overlapping occurrences by comparing characters, not slicing strings.
- Hex Checksum Medium Free Sum the bytes of a hex string with nibble arithmetic, not substrings.
- Most Common Word Length Medium Free Which word length appears most often? Count lengths, not words.