Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Order lines with product and category names

    MediumAbout 25 minFull AccessLearn it: Group sessions and join topic names

    The task

    The packing slip shows each order line with its product name and the product's category, so the packers know which shelf to go to.

    Complete Details(List<Line> lines, List<Product> products, List<Category> categories). A Line is a record with an OrderId, a ProductCode and a Quantity; a Product has a Code, a Name and a CategoryId; a Category has an Id and a Name. Return a LineDetail for each line, in the order of the lines, with its product's name and its product's category name. A line whose product code matches no product, or whose product's category matches no category, is dropped. The lists and their items are never null, product codes are unique, and category ids are unique.

    Signature

    public record Category(int Id, string Name);
    public record Product(string Code, string Name, int CategoryId);
    public record Line(int OrderId, string ProductCode, int Quantity);
    public record LineDetail(int OrderId, string Product, string Category, int Quantity);
    public static List<LineDetail> Details(List<Line> lines, List<Product> products, List<Category> categories)

    Examples

    • lines [(1, "P1", 2), (1, "P2", 1), (2, "P9", 4)], products [("P1", "pen", 10), ("P2", "mug", 20)], categories [(10, "office"), (20, "kitchen")] → [(1, "pen", "office", 2), (1, "mug", "kitchen", 1)]. P9 is not a known product, so its line is dropped.
    • lines [(3, "P3", 1)], products [("P3", "kite", 99)], categories [(10, "office")] → []. The kite's category, 99, is unknown, so the line is dropped.

    Limits: Up to 300,000 lines, 100,000 products and 1,000 categories.

    The tests check: no lines, an unknown product, a product with an unknown category, the lines keeping their order, several products in one category, categories matched by id and not by position, and 300,000 lines 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 Category(int Id, string Name);
      public record Product(string Code, string Name, int CategoryId);
      public record Line(int OrderId, string ProductCode, int Quantity);
      public record LineDetail(int OrderId, string Product, string Category, int Quantity);
      
      public static class Solution
      {
          // Each line with its product and category names; lines that cannot be matched are dropped.
          public static List<LineDetail> Details(List<Line> lines, List<Product> products, List<Category> categories)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.