Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Planned against actual hours

    MediumAbout 25 minFull AccessLearn it: SelectMany, Zip and Chunk: Flattening, Pairing and Batching Costs

    The task

    A weekly timesheet compares the hours planned for each day with the hours actually worked, and highlights the days that differ.

    Complete Compare(List<DayHours> planned, List<DayHours> actual). DayHours is a record with a Day and its Hours. Both lists name the same days in the same order, but the actual list may be shorter (the week is not over) or longer (extra days were worked). Pair the days by position, first with first, second with second, and compare only as many days as the shorter list has. Return a DayDifference for each paired day whose hours differ, in day order, with the planned day's name, both hours and Difference = actual minus planned. A day that went exactly to plan is left out. The lists and their items are never null.

    Signature

    public record DayHours(string Day, decimal Hours);
    public record DayDifference(string Day, decimal Planned, decimal Actual, decimal Difference);
    public static List<DayDifference> Compare(List<DayHours> planned, List<DayHours> actual)

    Examples

    • planned [("mon", 8), ("tue", 6), ("wed", 8)], actual [("mon", 9.5), ("tue", 6), ("wed", 7)] → [("mon", 8, 9.5, 1.5), ("wed", 8, 7, -1)]. Tuesday went exactly to plan, so it is left out; Wednesday was an hour short.
    • planned [("mon", 8), ("tue", 8)], actual [("mon", 8)] → []. Only Monday has both numbers, and it matched the plan.

    Limits: Up to 300,000 days in each list.

    The tests check: empty lists, an actual list shorter than the plan, an actual list longer than the plan, a negative difference, days in order, quarter hours, every day matching, and 300,000 days 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 DayHours(string Day, decimal Hours);
      public record DayDifference(string Day, decimal Planned, decimal Actual, decimal Difference);
      
      public static class Solution
      {
          // Pairs the days by position and lists the ones whose actual hours differ from the plan.
          public static List<DayDifference> Compare(List<DayHours> planned, List<DayHours> actual)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.