Hex Checksum
Medium About 15 min Free, no account needed
Sum the bytes of a hex string with nibble arithmetic, not substrings.
The brief
Given an even-length string of lowercase hex digits ("0a1f..."), interpret
each pair as a byte and return the sum of all byte values.
Substring(i, 2) + Convert.ToInt32(..., 16) allocates a string per byte.
The hidden test is a 100 KB hex string (50,000 bytes) under a 32 KB
allocation budget, so per-byte string allocation blows the budget. Watch your
allocations and process the characters in place.
Starter code
using System;
public class Solution
{
// TODO: return the sum of the byte values encoded in the hex string.
public long HexChecksum(string hex)
{
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 Substring Occurrences Medium Free Count overlapping occurrences by comparing characters, not slicing strings.
- Most Common Word Length Medium Free Which word length appears most often? Count lengths, not words.
- Count Words Easy Free Count words by spotting boundaries, not by splitting.
- Parse and Sum Integers Easy Free Sum space-separated integers (negatives included) allocation-free.