Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Order lines, even with unknown products

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

    The task

    The order page shows every line of an order, even when a product has been removed from the catalogue since the order was placed.

    Complete ShowLines(List<OrderLine> lines, List<Product> products). OrderLine is a record with a LineNo, a ProductCode and a Quantity; Product is a record with a Code, a Name and a Price. Return a LineView for every line, in the order of the lines: the product's Name, and a Total of Quantity times Price. A line whose product code is not in the product list shows "(unknown)" as the product and 0 as the total. Codes match exactly, letter case included. The lists and their items are never null, and product codes are unique.

    Signature

    public record Product(string Code, string Name, decimal Price);
    public record OrderLine(int LineNo, string ProductCode, int Quantity);
    public record LineView(int LineNo, string Product, decimal Total);
    public static List<LineView> ShowLines(List<OrderLine> lines, List<Product> products)

    Examples

    • lines [(1, "P1", 2), (2, "X9", 1), (3, "P2", 5)], products [("P1", "pen", 1.50), ("P2", "pad", 2.00)] → [(1, "pen", 3.00), (2, "(unknown)", 0), (3, "pad", 10.00)]. X9 is not in the catalogue, but its line is still shown.
    • lines [], products [("P1", "pen", 1.50)] → []. No lines, nothing to show.

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

    The tests check: no lines, an unknown product, an empty catalogue, the lines keeping their order, two lines for the same product, letter case in codes, and 300,000 lines against 100,000 products 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 Product(string Code, string Name, decimal Price);
      public record OrderLine(int LineNo, string ProductCode, int Quantity);
      public record LineView(int LineNo, string Product, decimal Total);
      
      public static class Solution
      {
          // Every line with its product name and total; "(unknown)" and 0 when the product is missing.
          public static List<LineView> ShowLines(List<OrderLine> lines, List<Product> products)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.