Trimmed Sum
Easy About 8 min Free, no account needed
Sum everything except one min and one max - without sorting a copy.
The brief
Given nums (at least three elements), return the sum of all elements after
removing one minimum and one maximum occurrence.
OrderBy(...).ToArray() (or sorting a clone) allocates a full copy of the
array. The hidden test streams 200,000 numbers under a 64 KB allocation
budget. You need the answer in a single pass with effectively zero
allocations, so sorting or copying the array is out.
Starter code
using System;
public class Solution
{
// TODO: return the sum after dropping one min and one max occurrence.
public long TrimmedSum(long[] nums)
{
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
- 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.
- Parse and Sum Integers Easy Free Sum space-separated integers (negatives included) allocation-free.
- Sum of Digits Easy Free Sum every digit of every number, without allocating along the way.
- Count Substring Occurrences Medium Free Count overlapping occurrences by comparing characters, not slicing strings.