Most Common Word Length
Medium About 15 min Free, no account needed
Which word length appears most often? Count lengths, not words.
The brief
Given text of words separated by spaces, return the word length that
occurs most often (ties go to the smaller length; 0 for an empty
text).
Splitting allocates every word just to read its .Length. The hidden test has
15,000 words under a 32 KB allocation budget, so splitting the text blows
the budget. You only need the lengths, not the words themselves, so watch your
allocations.
Starter code
using System;
using System.Collections.Generic;
public class Solution
{
// TODO: return the most frequent word length (ties to the smaller length).
public int MostCommonWordLength(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.
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.
- Hex Checksum Medium Free Sum the bytes of a hex string with nibble arithmetic, not substrings.
- 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.