Find each user's maximum number of concurrent sessions
Company: Pinterest
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Given a table of user sessions, find the maximum number of sessions each user had open at the same moment. Return `user_id` and `max_concurrent_sessions` for every user in the table.
Sessions are concurrent while their time ranges overlap. When one session starts at the exact timestamp another one ends, the start is counted before the end, so two back-to-back sessions reach a count of 2.
```hint Where the peak can happen
The number of open sessions only goes up when a session starts. Check the count at those moments, and decide carefully what happens when a start and an end share a timestamp.
```
### Input Table(s)
`sessions`
| Column | Type | Meaning |
|---|---|---|
| session_id | INT | Primary key |
| user_id | INT | The user who owns the session, never NULL |
| start_time | DATETIME (TIMESTAMP in PostgreSQL) | When the session started, never NULL |
| end_time | DATETIME (TIMESTAMP in PostgreSQL) | When the session ended, never NULL |
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | A user who has at least one session |
| max_concurrent_sessions | INTEGER | The largest number of that user's sessions open at one moment |
- One row per distinct `user_id` in `sessions`.
- Order by `user_id` ascending. Each user appears once, so this order is unique.
- No rounding is involved.
### Example
`sessions`
| session_id | user_id | start_time | end_time |
|---|---|---|---|
| 1 | 1 | 2024-01-01 08:00:00 | 2024-01-01 10:00:00 |
| 2 | 1 | 2024-01-01 09:00:00 | 2024-01-01 11:00:00 |
| 3 | 1 | 2024-01-01 09:30:00 | 2024-01-01 10:30:00 |
| 4 | 2 | 2024-01-01 08:00:00 | 2024-01-01 09:00:00 |
| 5 | 2 | 2024-01-01 09:00:00 | 2024-01-01 10:00:00 |
Expected result:
| user_id | max_concurrent_sessions |
|---|---|
| 1 | 3 |
| 2 | 2 |
At 09:30, all three of user 1's sessions are open. User 2's session 5 starts at 09:00, the same instant session 4 ends, and the tie rule counts that moment as two open sessions.
### Constraints and Clarifications
- `sessions` has at most 10,000 rows.
- `start_time < end_time` for every session.
- A session is open from its `start_time` through its `end_time`, both included, which is what makes back-to-back sessions count as concurrent.
- Only sessions of the same user are compared. Two sessions with identical start and end times are both counted.
- A user with a single session, or with sessions that never touch, has a maximum of 1.
Overview: Write a SQL query that finds, for each user, the largest number of their sessions open at the same moment, where back-to-back sessions count as overlapping. It tests interval overlap reasoning, window functions with careful tie ordering, and per-user aggregation over event timestamps.
Given a table of user sessions, find the maximum number of sessions each user had open at the same moment. Return `user_id` and `max_concurrent_sessions` for every user in the table.
Table `sessions` has one row per session:
- `session_id` (INTEGER): primary key.
- `user_id` (INTEGER): the user who owns the session, never NULL.
- `start_time` (TIMESTAMP): when the session started, never NULL.
- `end_time` (TIMESTAMP): when the session ended, never NULL.
Sessions are concurrent while their time ranges overlap. A session is open from its `start_time` through its `end_time`, both included. When one session starts at the exact timestamp another one ends, the start is counted before the end, so two back-to-back sessions reach a count of 2.
Clarifications:
- `start_time < end_time` for every session.
- Only sessions of the same user are compared.
- Two sessions with identical start and end times are both counted.
- A user with a single session, or with sessions that never touch, has a maximum of 1.
- `sessions` has at most 10,000 rows.
Output columns, in this order: `user_id` (INTEGER) and `max_concurrent_sessions` (INTEGER, the largest number of that user's sessions open at one moment). Return one row per distinct `user_id` in `sessions`, ordered by `user_id` ascending. Each user appears once, so this order is unique. No rounding is involved.
Tables
sessions(session_id INTEGER, user_id INTEGER, start_time TIMESTAMP, end_time TIMESTAMP)
Hints
- The number of open sessions only goes up when a session starts. Check the count at those moments, and decide carefully what happens when a start and an end share a timestamp.
- Every user in the table needs a row, including users whose sessions never overlap with another of their own sessions.