Find each user's maximum number of concurrent sessions

Quick 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.

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.

Pinterest logo
Pinterest
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

sessions

ColumnTypeMeaning
session_idINTPrimary key
user_idINTThe user who owns the session, never NULL
start_timeDATETIME (TIMESTAMP in PostgreSQL)When the session started, never NULL
end_timeDATETIME (TIMESTAMP in PostgreSQL)When the session ended, never NULL

Output Contract

ColumnTypeMeaning
user_idINTA user who has at least one session
max_concurrent_sessionsINTEGERThe 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_iduser_idstart_timeend_time
112024-01-01 08:00:002024-01-01 10:00:00
212024-01-01 09:00:002024-01-01 11:00:00
312024-01-01 09:30:002024-01-01 10:30:00
422024-01-01 08:00:002024-01-01 09:00:00
522024-01-01 09:00:002024-01-01 10:00:00

Expected result:

user_idmax_concurrent_sessions
13
22

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.