FastPrepCount Servers With No Requests in a Time Window

Count Servers With No Requests in a Time Window

IBM logoIBM● MediumFULLTIMEOA

Problem statement

There are n servers, numbered from 1 to n. You are given a 2D integer array logs, where logs[i] = [serverId, time] means that server serverId received a request at time time. You are also given an integer x and an integer array queries.

For each query queries[i], count the servers that received no request during the time window [queries[i] - x, queries[i]]. Both ends of the window are included.

Return an integer array result of the same length as queries, where result[i] is the answer for queries[i]. The logs are not sorted, and a server may have several logs, including identical ones.

Function

countServers(n: int, logs: int[][], x: int, queries: int[]) → int[]

Examples

Example 1

n = 4logs = [[1,2],[3,7],[2,9],[1,10]]x = 4queries = [8,13]return = [3,2]

For the query 8 the window is [4, 8]. Only server 3 has a request in it, at time 7, so servers 1, 2 and 4 received none and the answer is 3. For the query 13 the window is [9, 13], which holds the requests of server 2 at time 9 and server 1 at time 10. Servers 3 and 4 received none, so the answer is 2.

Example 2

n = 2logs = [[1,5],[2,10]]x = 5queries = [10,6,16]return = [0,1,2]

Both ends of a window are included. The window for 10 is [5, 10] and contains both requests, so the answer is 0. The window for 6 is [1, 6] and contains only the request of server 1, so the answer is 1. The window for 16 is [11, 16] and contains no request, so the answer is 2. The answers keep the order of queries.

Example 3

n = 3logs = [[2,4],[2,5],[2,6]]x = 2queries = [6]return = [2]

The window is [4, 6]. It contains three requests, but all of them come from server 2. Servers 1 and 3 received none, so the answer is 2.

Constraints

  • 1 <= n <= 10^5
  • 1 <= logs.length <= 2 * 10^4
  • logs[i].length == 2
  • 1 <= logs[i][0] <= n
  • 1 <= logs[i][1] <= 10^6
  • 1 <= x <= 10^5
  • 1 <= queries.length <= 1000
  • x < queries[i] <= 10^6

More IBM problems

See IBM hiring insights
Saved
public int[] countServers(int n, int[][] logs, int x, int[] queries) {
    // Write your code here.
}
Checking account…