Coding problem
Name the customer on each order
The task
The dispatch screen shows each order with the name of the customer who placed it.
Complete NameOrders(List<Customer> customers, List<Order> orders). Customer is a record with an Id and a Name; Order is a record with an Id, the CustomerId of the customer who placed it and a Total. Return a NamedOrder for each order whose customer is in the customer list, with the customer's name, in the order of the orders list. An order whose CustomerId matches no customer is left out. The lists and their items are never null, and customer ids are unique.
Signature
public record Customer(int Id, string Name); public record Order(int Id, int CustomerId, decimal Total); public record NamedOrder(int OrderId, string Customer, decimal Total); public static List<NamedOrder> NameOrders(List<Customer> customers, List<Order> orders)
Examples
customers [(1, "amy"), (2, "ben")], orders [(10, 2, 30), (11, 1, 15), (12, 3, 40)]→[(10, "ben", 30), (11, "amy", 15)]. Order 12 belongs to customer 3, who is not in the list, so it is left out.customers [], orders [(10, 1, 5)]→[]. No customer is known, so no order can be named.
Limits: Up to 200,000 customers and 300,000 orders.
The tests check: no orders, no known customers, an order of an unknown customer, the orders keeping their order, a customer with several orders, a customer without orders, two customers with the same name, and 300,000 orders against 200,000 customers 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 Customer(int Id, string Name);
public record Order(int Id, int CustomerId, decimal Total);
public record NamedOrder(int OrderId, string Customer, decimal Total);
public static class Solution
{
// Each order with its customer's name, in order-list order; orders of unknown customers are left out.
public static List<NamedOrder> NameOrders(List<Customer> customers, List<Order> orders)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.