Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    A live view or a snapshot?

    MediumAbout 25 minFull AccessLearn it: Choose a live query or a snapshot

    The task

    The support app has two screens. The live board must always show the tickets that are open right now; the end-of-shift report must keep the open tickets exactly as they were when the report was made. Both get the same list, which the app keeps changing afterwards: it adds tickets, and it closes a ticket by putting a closed copy in its place.

    Complete two methods. Ticket is a record with an Id, a Title and whether it is Open.

    • OpenTicketsLive(List<Ticket> tickets) returns a view: every time it is read, it gives the tickets that are open in the list at that moment, in list order.
    • OpenTicketsSnapshot(List<Ticket> tickets) returns a snapshot: the tickets that were open when the method was called, in list order, whatever happens to the list afterwards.

    An empty list gives empty results. The list and its tickets are never null, and nobody changes the list while a result is being read.

    Signature

    public record Ticket(int Id, string Title, bool Open);
    public static IEnumerable<Ticket> OpenTicketsLive(List<Ticket> tickets)
    public static IEnumerable<Ticket> OpenTicketsSnapshot(List<Ticket> tickets)

    Examples

    • [(1, "printer jam", open), (2, "vpn down", closed)], then (3, "new laptop", open) is added before reading → live view [1, 3], snapshot [1]. The view reads the list when it is read, so it sees ticket 3; the snapshot was taken before ticket 3 was added.
    • [(1, "printer jam", open)], then ticket 1 is closed before reading → live view [], snapshot [1]. The view sees that ticket 1 is closed now; the snapshot keeps it as it was.

    Limits: Up to 100,000 tickets.

    The tests check: a ticket added later, a ticket closed later, the list cleared later, the open tickets in list order, an empty list, the view read twice, and 100,000 tickets read through the view.

    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 Ticket(int Id, string Title, bool Open);
      
      public static class Solution
      {
          // A view: reads the list each time it is enumerated, so it shows the tickets open right now.
          public static IEnumerable<Ticket> OpenTicketsLive(List<Ticket> tickets)
          {
              throw new NotImplementedException();
          }
      
          // A snapshot: the tickets open now, copied into a new list that later changes do not touch.
          public static IEnumerable<Ticket> OpenTicketsSnapshot(List<Ticket> tickets)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.