Coding problem
The three most expensive products
The task
The shop's home page shows its three most expensive products, the priciest first.
Complete TopThree(List<Product> products). Product is a record with a Name and a Price. Return a new list of the three products with the highest prices, highest first; products with the same price are sorted by Name in ascending ordinal order, which compares characters by their numeric values. A list with fewer than three products gives all of them, in that order. Do not change the input list. The list and its products are never null, names hold only lowercase English letters, and prices are 0 or more.
Signature
public record Product(string Name, decimal Price); public static List<Product> TopThree(List<Product> products)
Examples
[("lamp", 40), ("desk", 250), ("pen", 2), ("chair", 120)]→[("desk", 250), ("chair", 120), ("lamp", 40)]. These are the three highest prices, highest first; the pen is left out.[("mug", 15), ("cup", 15)]→[("cup", 15), ("mug", 15)]. There are only two products, and they cost the same, so their names decide.
Limits: Up to 200,000 products.
The tests check: an empty list, one product, fewer than three, exactly three, the highest prices first, a tie for third place settled by name, equal prices sorted by name, prices with cents, that the input list is not changed, and 200,000 products in time.
Hints
The solution opens by itself when all tests pass. You remember more if you try first.
Sign in to see the solutionYour 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 Product(string Name, decimal Price);
public static class Solution
{
// Returns the three most expensive products, highest price first, then by name.
public static List<Product> TopThree(List<Product> products)
{
throw new NotImplementedException();
}
}Sign in to run and submit your code. It stays here while you do.