Peak number of concurrent sessions
HardP3Coding
Table sessions:
| Column | Type |
|---|---|
| id | int (primary key) |
| start_at | timestamp |
| end_at | timestamp (after start_at) |
Find the peak number of sessions running at the same time. A session that ends exactly when another starts is not running at the same time as it. For an empty table the peak is 0.
Output: one row, column peak.
Schema
CREATE TABLE sessions ( id int PRIMARY KEY, start_at timestamp NOT NULL, end_at timestamp NOT NULL CHECK (end_at > start_at) );
Example 1
- Input
- sessions: 10:00-12:00, 10:30-11:30, 11:00-13:00
- Output
- peak: 3
- Why
- At 11:00 all three are running.
Example 2
- Input
- sessions: 10:00-11:00, 11:00-12:00
- Output
- peak: 1
Constraints
- PostgreSQL dialect.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 3 visible tests, Submit all 7. ⌘Enter runs, ⌘⇧Enter submits.