Coding problem
Why does this query run twice?
The task
The audit job receives a day's account movements, in cents, from a feed that can be read only once, like a network stream: reading it a second time throws an InvalidOperationException.
Complete Summarise(IEnumerable<int> movements). Return a Summary with the number of movements in Count, their sum in Total, the largest movement in Highest (null when there are none), and in AboveAverage the movements strictly greater than the average, in feed order. The average is the exact Total divided by Count, not rounded and not cut to a whole number. Movements can be negative, and their sum can be far beyond an int. With no movements, return a count and total of 0, no highest, and an empty list. The feed is never null.
Signature
public record Summary(int Count, long Total, int? Highest, List<int> AboveAverage); public static Summary Summarise(IEnumerable<int> movements)
Examples
[5, -3, 10, 4]→(4, 16, 10, [5, 10]). The average is 16 / 4 = 4, and only 5 and 10 are above it.[]→(0, 0, null, []). An empty day has nothing to summarise.
Limits: Up to 1,000,000 movements, each from -2,000,000,000 to 2,000,000,000.
The tests check: a feed read only once in every test, an empty feed, a total beyond an int, an average that is not a whole number with negative movements, movements equal to the average, a negative highest, the feed order kept, one movement, and 1,000,000 movements in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public record Summary(int Count, long Total, int? Highest, List<int> AboveAverage);
public static class Solution
{
// Count, total, highest and the movements above the average, reading the feed exactly once.
public static Summary Summarise(IEnumerable<int> movements)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.