Skip to main content

Server Utilization Time - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase5 min read
Practice this problem

Problem Statement

Table: Servers

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| server_id      | int      |
| status_time    | datetime |
| session_status | enum     |
+----------------+----------+
(server_id, status_time, session_status) is the primary key (combination of columns with unique values) for this table.
session_status is an ENUM (category) type of ('start', 'stop').
Each row of this table contains server_id, status_time, and session_status.

Write a solution to find the total time when servers were running. The output should be rounded down to the nearest number of full days.

Return the result table in any order.

The result format is in the following example.

 

Example:

Input:

Servers table:

+-----------+---------------------+----------------+
| server_id | status_time         | session_status |
+-----------+---------------------+----------------+
| 3         | 2023-11-04 16:29:47 | start          |
| 3         | 2023-11-05 01:49:47 | stop           |
| 3         | 2023-11-25 01:37:08 | start          |
| 3         | 2023-11-25 03:50:08 | stop           |
| 1         | 2023-11-13 03:05:31 | start          |
| 1         | 2023-11-13 11:10:31 | stop           |
| 4         | 2023-11-29 15:11:17 | start          |
| 4         | 2023-11-29 15:42:17 | stop           |
| 4         | 2023-11-20 00:31:44 | start          |
| 4         | 2023-11-20 07:03:44 | stop           |
| 1         | 2023-11-20 00:27:11 | start          |
| 1         | 2023-11-20 01:41:11 | stop           |
| 3         | 2023-11-04 23:16:48 | start          |
| 3         | 2023-11-05 01:15:48 | stop           |
| 4         | 2023-11-30 15:09:18 | start          |
| 4         | 2023-11-30 20:48:18 | stop           |
| 4         | 2023-11-25 21:09:06 | start          |
| 4         | 2023-11-26 04:58:06 | stop           |
| 5         | 2023-11-16 19:42:22 | start          |
| 5         | 2023-11-16 21:08:22 | stop           |
+-----------+---------------------+----------------+

Output:

+-------------------+
| total_uptime_days |
+-------------------+
| 1                 |
+-------------------+

Explanation:

  • For server ID 3:
    • From 2023-11-04 16:29:47 to 2023-11-05 01:49:47: ~9.3 hours
    • From 2023-11-25 01:37:08 to 2023-11-25 03:50:08: ~2.2 hours
    • From 2023-11-04 23:16:48 to 2023-11-05 01:15:48: ~1.98 hours
    Total for server 3: ~13.48 hours
  • For server ID 1:
    • From 2023-11-13 03:05:31 to 2023-11-13 11:10:31: ~8 hours
    • From 2023-11-20 00:27:11 to 2023-11-20 01:41:11: ~1.23 hours
    Total for server 1: ~9.23 hours
  • For server ID 4:
    • From 2023-11-29 15:11:17 to 2023-11-29 15:42:17: ~0.52 hours
    • From 2023-11-20 00:31:44 to 2023-11-20 07:03:44: ~6.53 hours
    • From 2023-11-30 15:09:18 to 2023-11-30 20:48:18: ~5.65 hours
    • From 2023-11-25 21:09:06 to 2023-11-26 04:58:06: ~7.82 hours
    Total for server 4: ~20.52 hours
  • For server ID 5:
    • From 2023-11-16 19:42:22 to 2023-11-16 21:08:22: ~1.43 hours
    Total for server 5: ~1.43 hours
The accumulated runtime for all servers totals approximately 44.46 hours, equivalent to one full day plus some additional hours. However, since we consider only full days, the final output is rounded to 1 full day.

Approach Overview

Problem Overview: Each row represents a server event (start or stop) with a timestamp. The task is to compute the total time each server was actively running by pairing every start event with its corresponding stop event and summing the duration.

Approach 1: Self Join Event Pairing (O(n log n) time, O(n) space)

One straightforward SQL strategy pairs start rows with the next stop row for the same server using a self join. You join the table with itself on server_id and ensure the stop timestamp is greater than the start timestamp. Then filter to keep the nearest stop event and compute stop_time - start_time. Finally, aggregate the durations with SUM() grouped by server. This works but requires extra filtering logic to ensure the correct pairing and usually involves sorting or subqueries.

Because every event may compare with multiple later rows, the query becomes heavier on large logs. The logic is also harder to maintain when event ordering matters. This method still works in relational systems without strong window function support.

Approach 2: Window Function Pairing with LEAD (O(n log n) time, O(n) space)

The clean solution uses SQL window functions. Partition the events by server_id and order them by timestamp. Apply LEAD(timestamp) to fetch the timestamp of the next event for the same server. When the current row is a start event, the next row should be the corresponding stop. The difference between these timestamps gives the utilization duration for that session.

After computing these session durations, aggregate them with SUM() per server. Window functions handle event pairing in a single pass over each server's ordered events, making the query concise and reliable. This approach is the standard pattern for event log problems involving start/stop intervals.

This technique relies on ordered partitions and sequential access, which is exactly what SQL window functions are designed for. Problems involving time intervals, sessionization, or log processing frequently use LEAD or LAG from window functions inside relational databases. Efficient querying and aggregation like this are core patterns in database interview questions.

Recommended for interviews: The window function solution is what interviewers typically expect. It shows you understand event sequencing and modern SQL features. Explaining the self-join approach first demonstrates reasoning about pairing events, but implementing it with LEAD shows stronger SQL fluency and cleaner query design.

Solution

We can use the window function LEAD to get the time of the next status for each server. The time difference between two statuses is the running time of the server. Finally, we add up the running time of all servers, then divide by the number of seconds in a day to get the total running days of the servers.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Self Join Event PairingO(n log n)O(n)When window functions are unavailable or when using older SQL systems
Window Functions with LEADO(n log n)O(n)Best approach for ordered event logs and modern SQL databases like MySQL/PostgreSQL

Video Solution

Leetcode MEDIUM 3126 - Server Utilization Time CTEs SELF JOIN - Explained by Everyday Data Science • Everyday Data Science • 666 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Server Utilization Time easy or hard?
Server Utilization Time is generally considered a medium-level database problem. The main challenge is recognizing that start and stop events must be paired in order. Once you apply a window function like LEAD(), the implementation becomes straightforward.
Server Utilization Time Python/Java solution
This problem is primarily designed for SQL because the data is stored in a relational table of events. A Python or Java solution would typically load the events, sort them by server and timestamp, then iterate through them to pair start and stop events and accumulate durations.
How to solve Server Utilization Time in O(n)?
Pure O(n) processing is difficult in SQL because ordered window functions require sorting. The closest practical approach uses LEAD() over server partitions ordered by timestamp, which becomes O(n log n) internally. After pairing start and stop rows, compute the duration difference and sum it per server.
What is the best approach for Server Utilization Time?
The best approach uses SQL window functions, specifically LEAD(), to pair each start event with the next stop event for the same server. By partitioning rows by server_id and ordering by timestamp, you can directly compute session durations. This avoids complex joins and keeps the query concise while maintaining O(n log n) performance due to sorting.
Is Server Utilization Time asked at Google/Amazon/Meta?
Log processing and session duration problems like Server Utilization Time appear frequently in database and data engineering interviews. Companies such as Google, Amazon, and Meta often test SQL skills involving event logs, window functions, and time interval aggregation.
What data structure is used in Server Utilization Time?
The problem relies on relational table processing with ordered partitions rather than traditional data structures. SQL window functions simulate sequential access to rows, allowing each event to reference the next event using LEAD() or the previous event using LAG().
What is the time complexity of Server Utilization Time?
The typical SQL solution runs in O(n log n) time because the database must sort events by timestamp within each server partition. Window functions like LEAD() then process rows sequentially. Space complexity is O(n) due to intermediate query processing and result aggregation.

Ready to solve this problem?

Practice Server Utilization Time with our built-in code editor and test cases.

Practice on FleetCode