Skip to content
Katabench
Try free

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.

The allocation budget

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

C#
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.

C# coding challenges →

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