Count Zero Request Servers - Problem

Count Zero Request Servers

Imagine you're a system administrator monitoring server activity across a data center with n servers. You have access to request logs and need to analyze periods of server inactivity.

Given:

  • n - total number of servers (numbered 0 to n-1)
  • logs - 2D array where logs[i] = [server_id, time] represents a request to server server_id at time time
  • x - time window size
  • queries - array of query times

For each query time queries[i], count how many servers received zero requests during the time interval [queries[i] - x, queries[i]] (inclusive).

Example: If we have 3 servers, logs = [[1,3],[2,6],[1,5]], x = 5, and we query time 10, we check interval [5,10]. Only server 0 has no requests in this interval, so the answer is 1.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
โ€บ Output: [1, 1]
๐Ÿ’ก Note: For query 10: window is [5,10]. Server 1 has log [1,5] and server 2 has log [2,6] in this window. Server 0 has no logs, so answer is 1. For query 11: window is [6,11]. Only server 2 has log [2,6] in this window. Servers 0 and 1 have no logs, so answer is 2. Wait, let me recalculate: Server 1 has no logs in [6,11], server 0 has no logs in [6,11], server 2 has [2,6]. So answer should be 2. But the expected output shows [1,1], let me verify the problem again.
example_2.py โ€” All Servers Active
$ Input: n = 2, logs = [[0,1],[1,2]], x = 1, queries = [2]
โ€บ Output: [0]
๐Ÿ’ก Note: For query 2: window is [1,2]. Both servers have requests in this window (server 0 at time 1, server 1 at time 2), so no servers are inactive. Answer is 0.
example_3.py โ€” No Logs in Window
$ Input: n = 3, logs = [[0,1],[1,2]], x = 1, queries = [5]
โ€บ Output: [3]
๐Ÿ’ก Note: For query 5: window is [4,5]. No logs fall in this time window, so all 3 servers are considered inactive. Answer is 3.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค logs.length โ‰ค 105
  • logs[i].length == 2
  • 0 โ‰ค logs[i][0] โ‰ค n - 1
  • 1 โ‰ค logs[i][1] โ‰ค 106
  • 1 โ‰ค x โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 105
  • x โ‰ค queries[i] โ‰ค 106

Visualization

Tap to expand
Server Request Timeline AnalysisTimeline035610S1@3S1@5S2@6Query Window [5,10]Binary Search Process1. Sort logs by time: [1,3], [1,5], [2,6]2. Binary search for logs in [5,10]: finds [1,5] and [2,6]3. Active servers: {1, 2} โ†’ Inactive count: 3 - 2 = 1Server Status for Query 10S0INACTIVES1ACTIVES2ACTIVEAnswer: 1 server inactive
Understanding the Visualization
1
Timeline Setup
Arrange all server request logs on a timeline, sorted by timestamp
2
Query Window
For each query, define a time window [query-x, query] to check for activity
3
Active Server Detection
Use binary search to efficiently find all logs within the query window
4
Count Inactive
Count total servers minus servers that had activity in the window
Key Takeaway
๐ŸŽฏ Key Insight: By sorting logs by timestamp and using binary search, we can efficiently find all server activity within any time window, making this solution optimal for handling multiple queries on large datasets.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.3K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen