Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Total and average sale per region

    EasyAbout 15 minFull AccessLearn it: Group sessions and join topic names

    The task

    The monthly report shows, for each sales region, how many sales it made, their total and the average sale.

    Complete PerRegion(List<Sale> sales). Sale is a record with a Region and an Amount. Return one RegionTotals for each region that has sales: Sales is the number of its sales, Total their sum and Average the total divided by the number of sales, as an exact decimal, not rounded. Sort the rows by Region in ascending ordinal order, which compares characters by their numeric values. An empty list gives an empty list. The list and its sales are never null, regions hold only lowercase English letters, and amounts are more than 0.

    Signature

    public record Sale(string Region, decimal Amount);
    public record RegionTotals(string Region, int Sales, decimal Total, decimal Average);
    public static List<RegionTotals> PerRegion(List<Sale> sales)

    Examples

    • [("north", 100), ("south", 40), ("north", 50)] → [("north", 2, 150, 75), ("south", 1, 40, 40)]. north made two sales, 150 in all and 75 on average.
    • [] → []. No sales, no regions.

    Limits: Up to 300,000 sales in up to 30,000 regions.

    The tests check: an empty list, regions sorted A to Z, a region with one sale, an average with cents, amounts with cents adding up exactly, and 300,000 sales in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      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 Region, decimal Amount);
      public record RegionTotals(string Region, int Sales, decimal Total, decimal Average);
      
      public static class Solution
      {
          // For each region: its number of sales, their total and their average, regions A to Z.
          public static List<RegionTotals> PerRegion(List<Sale> sales)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.