Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Each customer with their orders

    MediumAbout 25 minFull AccessLearn it: Nested Queries O(n·m) vs Join and HashSet O(n+m)

    The task

    The account page lists every customer with the numbers of their orders, the newest order first.

    Complete OrdersByCustomer(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 the Day it was placed. Return one CustomerOrders for every customer, in the order of the customer list, with the ids of that customer's orders: the latest Day first, and orders on the same day by Id, smallest first. A customer with no orders gets an empty list. An order whose CustomerId matches no customer is ignored. 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, int Day);
    public record CustomerOrders(string Customer, List<int> OrderIds);
    public static List<CustomerOrders> OrdersByCustomer(List<Customer> customers, List<Order> orders)

    Examples

    • customers [(1, "amy"), (2, "ben"), (3, "cara")], orders [(10, 1, 5), (11, 3, 2), (12, 1, 9)] → [("amy", [12, 10]), ("ben", []), ("cara", [11])]. amy's order 12 (day 9) is newer than order 10 (day 5), and ben has no orders.
    • customers [(1, "amy")], orders [(10, 4, 1)] → [("amy", [])]. Order 10 belongs to customer 4, who is not in the list, so it is ignored.

    Limits: Up to 100,000 customers and 300,000 orders.

    The tests check: no customers, a customer without orders, orders of unknown customers, customers in their list order, orders newest first, orders on the same day by id, each order going to its own customer, and 300,000 orders for 100,000 customers in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      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, int Day);
      public record CustomerOrders(string Customer, List<int> OrderIds);
      
      public static class Solution
      {
          // Every customer in list order with their order ids, newest day first, then by id.
          public static List<CustomerOrders> OrdersByCustomer(List<Customer> customers, List<Order> orders)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.