Coding problem
Total spent per customer
The task
The loyalty team invites the customers who spent at least a set amount this year.
Complete BigSpenders(List<Payment> payments, decimal threshold). Payment is a record with a Customer and an Amount; a refund is a payment with a negative amount. A customer's total is the sum of all their payments, refunds included. Return a CustomerTotal for each customer whose total is at least threshold, the biggest total first; customers with the same total are sorted by name in ascending ordinal order, which compares characters by their numeric values. An empty list gives an empty list. The list and its payments are never null, and customer names hold only lowercase English letters.
Signature
public record Payment(string Customer, decimal Amount); public record CustomerTotal(string Customer, decimal Total); public static List<CustomerTotal> BigSpenders(List<Payment> payments, decimal threshold)
Examples
payments [("amy", 120), ("ben", 80), ("amy", -30), ("cara", 95)], threshold 90→[("cara", 95), ("amy", 90)]. amy spent 120 and got 30 back, exactly 90, so she is kept; ben's 80 is below the threshold.payments [("dev", 50)], threshold 100→[]. Nobody reaches 100.
Limits: Up to 400,000 payments from up to 50,000 customers.
The tests check: an empty list, nobody at the threshold, a total exactly at the threshold, a refund lowering a total, small payments adding up past the threshold, equal totals sorted by name, a threshold of 0, and 400,000 payments 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 Payment(string Customer, decimal Amount);
public record CustomerTotal(string Customer, decimal Total);
public static class Solution
{
// Each customer's total, refunds included; those at or above the threshold, biggest first, then by name.
public static List<CustomerTotal> BigSpenders(List<Payment> payments, decimal threshold)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.