You are given an integer n
denoting the total number of servers and a 2D 0-indexed integer array logs
, where logs[i] = [server_id, time]
denotes that the server with id server_id
received a request at time time
.
You are also given an integer x
and a 0-indexed integer array queries
.
Return a 0-indexed integer array arr
of length queries.length
where arr[i]
represents the number of servers that did not receive any requests during the time interval [queries[i] - x, queries[i]]
.
Note that the time intervals are inclusive.
Example 1:
Input: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11] Output: [1,2] Explanation: For queries[0]: The servers with ids 1 and 2 get requests in the duration of [5, 10]. Hence, only server 3 gets zero requests. For queries[1]: Only the server with id 2 gets a request in duration of [6,11]. Hence, the servers with ids 1 and 3 are the only servers that do not receive any requests during that time period.
Example 2:
Input: n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4] Output: [0,1] Explanation: For queries[0]: All servers get at least one request in the duration of [1, 3]. For queries[1]: Only server with id 3 gets no request in the duration [2,4].
Constraints:
1 <= n <= 105
1 <= logs.length <= 105
1 <= queries.length <= 105
logs[i].length == 2
1 <= logs[i][0] <= n
1 <= logs[i][1] <= 106
1 <= x <= 105
x < queries[i] <= 106
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We have servers and requests. We want to find, for each server, how many requests fall within its active time window. The brute force method checks every possible request for every server to see if it falls within the server's time window.
Here's how the algorithm would work step-by-step:
def count_zero_request_servers_brute_force(servers, requests):
server_request_counts = []
for server_index in range(len(servers)):
request_count_for_server = 0
# Iterate through each request to check its relevance to the current server
for request_index in range(len(requests)):
request_time = requests[request_index]
server_start_time = servers[server_index][0]
server_end_time = servers[server_index][1]
# Checks if the request falls within the server's active time window
if server_start_time <= request_time <= server_end_time:
request_count_for_server += 1
server_request_counts.append(request_count_for_server)
return server_request_counts
We need to figure out, for each server, how many requests it didn't handle (requests equal to zero). The efficient way is to pre-calculate the number of requests each server handles up to a certain time, then quickly find the number of zero requests for each server by looking at the right time range.
Here's how the algorithm would work step-by-step:
def count_zero_request_servers(number_of_servers, logs, ranges):
running_totals = [([0] * (len(logs) + 1)) for _ in range(number_of_servers)]
for server_index in range(number_of_servers):
for log_index in range(len(logs)):
running_totals[server_index][log_index + 1] = running_totals[server_index][log_index] + (1 if logs[log_index][0] == server_index else 0)
zero_request_counts = [0] * number_of_servers
# Iterate through each server.
for server_index in range(number_of_servers):
# Check each time range for the current server.
for time_range in ranges:
start_time, end_time = time_range
# Calculate requests handled in the given time range.
requests_handled = running_totals[server_index][end_time + 1] - running_totals[server_index][start_time]
# Increment counter if no requests were handled.
if requests_handled == 0:
zero_request_counts[server_index] += 1
return zero_request_counts
Case | How to Handle |
---|---|
Empty arrival or departure array | Return an empty list since no requests can be processed with empty input. |
Null arrival or departure array | Throw an IllegalArgumentException or return null to indicate invalid input. |
Arrival and departure arrays have different lengths | Throw an IllegalArgumentException indicating that the arrays must be of equal size. |
q array is empty | Return a list of zeros of the same size, because no requests will be counted since there are no queries. |
Large input arrays (arrival, departure, q) | Ensure the solution's time complexity is efficient (e.g., O(n log n)) to handle large inputs without exceeding time limits. |
Queries outside the range of arrival/departure times | Correctly handle these by considering all arrival/departure times when they are inside each query range. |
Overlapping intervals in arrival and departure arrays. | The solution should correctly count the active servers at each query time regardless of overlapping intervals. |
Queries with very close or identical values | Verify the inclusiveness/exclusiveness of the query range endpoints is handled correctly (i.e., [start, end]). |