Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    How long until every server has the update

    HardAbout 40 minFull AccessLearn it: Topological Sort, Dijkstra with PriorityQueue and Union-Find in C#

    The task

    A config update spreads from one server to the others over one-way links, and each link takes some milliseconds.

    Servers are numbered 0 to n − 1. A link [from, to, time] carries the update from from to to in time milliseconds, with time ≥ 0. The update starts at source at time 0, and a server passes it on as soon as it has it. Complete TimeToReachAll(int n, int[][] edges, int source): return the time when the last server receives it, or −1 if some server never does. Return a long: times add up along a path.

    Signature

    public static long TimeToReachAll(int n, int[][] edges, int source)

    Examples

    • 3 servers, links [[0, 1, 10], [0, 2, 1], [2, 1, 2]], source 0 → 3. Server 1 gets it at 3 through server 2, sooner than over the direct link of 10.
    • 2 servers, links [[1, 0, 5]], source 0 → -1. The only link goes the other way.

    Limits: Up to 100,000 servers and 200,000 links; a link can take up to int.MaxValue milliseconds.

    The tests check: one server, a server with no link, the direction of links, zero-time links, parallel links, a loop, a source other than 0, totals beyond int, a longer path that is faster, and 100,000 servers 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 static class Solution
      {
          // Links are [from, to, time]; returns when the last server receives the update, or -1.
          public static long TimeToReachAll(int n, int[][] edges, int source)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.