Coding problem
Orders per customer
The task
The sales team wants to see which customers order most often.
Complete OrdersPerCustomer(List<Order> orders). Order is a record with an Id, a Customer and an Amount. Return one CustomerCount for each customer who has orders, with the number of orders in Orders. Put the most orders first; customers with the same number of orders are sorted by name in ascending ordinal order, which compares characters by their numeric values. The amounts do not matter here. An empty list gives an empty list. The list and its orders are never null, and customer names hold only lowercase English letters.
Signature
public record Order(int Id, string Customer, decimal Amount); public record CustomerCount(string Customer, int Orders); public static List<CustomerCount> OrdersPerCustomer(List<Order> orders)
Examples
[(1, "amy", 20), (2, "ben", 5), (3, "amy", 12), (4, "cara", 8), (5, "ben", 30)]→[("amy", 2), ("ben", 2), ("cara", 1)]. amy and ben both have two orders, so their names decide; cara has one.[]→[]. No orders, no customers.
Limits: Up to 300,000 orders from up to 50,000 customers.
The tests check: an empty list, one order, the count deciding before the name, equal counts sorted by name, each customer once, amounts not mattering, one customer with many orders, and 300,000 orders 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 Order(int Id, string Customer, decimal Amount);
public record CustomerCount(string Customer, int Orders);
public static class Solution
{
// The number of orders per customer, most orders first, then by name.
public static List<CustomerCount> OrdersPerCustomer(List<Order> orders)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.