Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    A price lookup by product code

    MediumAbout 25 minFull AccessLearn it: Hashing Operators: GroupBy, ToLookup, ToDictionary, CountBy and AggregateBy

    The task

    The till looks up prices by product code. The price list is a file of rows in the order they were entered, and a later row for a code is a price update.

    Complete PriceLookup(List<PriceRow> rows). PriceRow is a record with a Code and a Price. Return a dictionary from code to price with one entry per code. Codes ignore letter case: "AB-1" and "ab-1" are the same code, and the dictionary must find a code written in any letter case. When a code has several rows, the price of the last row wins. An empty list gives an empty dictionary. The list and its rows are never null, and codes are never empty.

    Signature

    public record PriceRow(string Code, decimal Price);
    public static Dictionary<string, decimal> PriceLookup(List<PriceRow> rows)

    Examples

    • [("AB-1", 10), ("cd-2", 25), ("ab-1", 12)] → {"AB-1": 12, "cd-2": 25}. "ab-1" is the same code as "AB-1" and comes later, so its price, 12, wins.
    • [] → {}. An empty price list gives an empty lookup.

    Limits: Up to 300,000 rows.

    The tests check: an empty list, a later row replacing an earlier one, three rows for one code, lookups in any letter case, different codes keeping their own prices, the number of entries, an unknown code not found, and 300,000 rows 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 PriceRow(string Code, decimal Price);
      
      public static class Solution
      {
          // Code to price, codes ignoring letter case, the last row for a code winning.
          public static Dictionary<string, decimal> PriceLookup(List<PriceRow> rows)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.