Coding problem
The best seller in each category
The task
The shop's weekly report names the best seller of each category: the product that sold the most units over the week.
Complete BestSellers(List<DaySale> sales). A DaySale is a record with a Category, a Product and the Units sold on one day; a product can have a row for several days, and a product always belongs to the same category. A product's units are the sum over all its rows. Return one BestSeller for each category: the product with the most units and that number of units. When products tie, the one whose name comes first in ascending ordinal order wins. Sort the result by Category in ascending ordinal order. An empty list gives an empty list. The list and its rows are never null, names hold only lowercase English letters, and units are 0 or more.
Signature
public record DaySale(string Category, string Product, int Units); public record BestSeller(string Category, string Product, int Units); public static List<BestSeller> BestSellers(List<DaySale> sales)
Examples
[("toys", "kite", 3), ("books", "atlas", 5), ("toys", "yoyo", 4), ("toys", "kite", 2)]→[("books", "atlas", 5), ("toys", "kite", 5)]. kite sold 3 + 2 = 5 units, more than yoyo's 4; books come before toys.[("tea", "mint", 6), ("tea", "chai", 6)]→[("tea", "chai", 6)]. Both sold 6 units, so chai wins: its name comes first.
Limits: Up to 300,000 rows.
The tests check: an empty list, one row, a product's days added before comparing, many small days beating one big day, a tie settled by name and not by list order, categories sorted A to Z, one row per category, and 300,000 rows 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 DaySale(string Category, string Product, int Units);
public record BestSeller(string Category, string Product, int Units);
public static class Solution
{
// For each category, the product with the most units over all its days (ties by name), categories A to Z.
public static List<BestSeller> BestSellers(List<DaySale> sales)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.