FastPrepPing Reply Tracking
Problem · Array

Ping Reply Tracking

Learn this problem
MediumNvidia logoNvidiaFULLTIMEONSITE INTERVIEW

Problem statement

Track replies to a finite set of ping echo requests. For this exercise, assume request i is sent at integer time sendTimes[i] and carries the unique identifier i. The request identifiers are 0 through sendTimes.length - 1.

The parallel arrays replyIds and replyTimes describe received echo replies. Reply j names request replyIds[j] and arrives at time replyTimes[j]. The arrays may list replies in any order.

  • A reply is eligible only when its identifier names an existing request and its arrival time is in the inclusive interval [sendTimes[i], sendTimes[i] + timeout].
  • Ignore unknown identifiers, replies before a request was sent, and replies after its deadline.
  • When several replies are eligible for one request, use the earliest arrival time. Later duplicates have no effect.

Return one integer per request, in request-identifier order. An answered request returns its round-trip time, earliestArrival - sendTimes[i]. A request with no eligible reply returns -1. This exercise supplies the finite send and receive trace; no network access or packet construction is required.

Function

pingRoundTripTimes(sendTimes: int[], replyIds: int[], replyTimes: int[], timeout: int) → int[]

Examples

Example 1

sendTimes = [10,20,30]replyIds = [1,0,1,2]replyTimes = [25,17,22,41]timeout = 10return = [7,2,-1]

Request 1 chooses time 22 even though its time-25 reply appears earlier in the array. Request 2 times out because 41 is later than its deadline 40.

Example 2

sendTimes = [5,5]replyIds = [0,1,2,0]replyTimes = [4,5,5,5]timeout = 0return = [0,0]

With a zero timeout, only replies exactly at the send time qualify. The early reply and unknown identifier are ignored.

Example 3

sendTimes = [100,50]replyIds = []replyTimes = []timeout = 8return = [-1,-1]

No received replies means both requests time out, regardless of send-time order.

Constraints

  • 1 <= sendTimes.length <= 10^5.
  • 0 <= replyIds.length = replyTimes.length <= 10^5.
  • 0 <= sendTimes[i], replyTimes[j] <= 10^9.
  • -1 <= replyIds[j] <= 10^5.
  • 0 <= timeout <= 10^6.
  • Send times need not be sorted or unique.

More Nvidia problems

drafts saved locally
public int[] pingRoundTripTimes(int[] sendTimes, int[] replyIds, int[] replyTimes, int timeout) {
    // Write your code here.
}
sendTimes[10,20,30]
replyIds[1,0,1,2]
replyTimes[25,17,22,41]
timeout10
expected[7,2,-1]
checking account