Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The first and last sign-in

    EasyAbout 15 minFull AccessLearn it: OrderBy Under the Hood: O(n log n), an O(n) Buffer, Stability and Top-k

    The task

    The security page shows who signed in first and who signed in last today.

    Complete FirstAndLast(List<SignIn> signIns). SignIn is a record with a User and the Minute of the day the user signed in; the list is not in time order. Return a DayEnds whose First is the sign-in with the smallest Minute and whose Last is the sign-in with the largest Minute. When several sign-ins share the smallest minute, First is the one that comes first in the list; when several share the largest minute, Last is also the one that comes first in the list. With no sign-ins, both are null. The list and its sign-ins are never null, and minutes are from 0 to 1,439.

    Signature

    public record SignIn(string User, int Minute);
    public record DayEnds(SignIn? First, SignIn? Last);
    public static DayEnds FirstAndLast(List<SignIn> signIns)

    Examples

    • [("ben", 540), ("amy", 495), ("cara", 1020), ("dev", 495)] → (("amy", 495), ("cara", 1020)). amy and dev both signed in at minute 495, and amy comes first in the list.
    • [] → (null, null). There were no sign-ins.

    Limits: Up to 500,000 sign-ins.

    The tests check: no sign-ins, one sign-in, a list not in time order, a tie for the earliest, a tie for the latest, minute 0, and 500,000 sign-ins 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 SignIn(string User, int Minute);
      public record DayEnds(SignIn? First, SignIn? Last);
      
      public static class Solution
      {
          // The earliest and the latest sign-in; on a tie, the one that comes first in the list; null when there are none.
          public static DayEnds FirstAndLast(List<SignIn> signIns)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.