Coding problem
Totals that survive empty days and big numbers
The task
The shop's end-of-day screen shows the day's total, its average sale and its biggest sale, in cents. It must work on a quiet day with no sales and on a busy day of very large sales.
Complete Totals(List<Sale> sales). Sale is a record with an Item and its price in Cents. Return a DayTotals. TotalCents is the sum of all the sales, as a long. AverageCents is the total divided by the number of sales, with its fraction, or 0 when there are no sales. HighestCents is the biggest sale, or null when there are no sales. Never throw. The list and its sales are never null, and each sale is from 0 to 2,000,000,000 cents.
Signature
public record Sale(string Item, int Cents); public record DayTotals(long TotalCents, double AverageCents, int? HighestCents); public static DayTotals Totals(List<Sale> sales)
Examples
[("tv", 2_000_000_000), ("fridge", 1_500_000_000)]→(3_500_000_000, 1_750_000_000, 2_000_000_000). The total does not fit in an int, so it is a long.[]→(0, 0, null). A day with no sales has a total and an average of 0 and no biggest sale.
Limits: Up to 1,000,000 sales.
The tests check: a day with no sales, a busy day whose total passes int.MaxValue, an ordinary day, one sale, the biggest sale first in the list, an average with a fraction, and 1,000,000 big sales 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 Sale(string Item, int Cents);
public record DayTotals(long TotalCents, double AverageCents, int? HighestCents);
public static class Solution
{
// The day's total (as long), average (0 on an empty day) and biggest sale (null on an empty day), never throwing.
public static DayTotals Totals(List<Sale> sales)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.