Back to Practice
#0358

Simple Sessionization by Gap

HardSQL35 min25 XP

Problem

For one user, return event_id and is_new_session where a new session starts if previous event is NULL or gap is more than 30 minutes.

Why This Matters

SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.

Schema

events(event_id INTEGER, event_time TEXT)

Examples

Example 1
Inputevents at 10:00, 10:20, 11:00
Outputnew, same, new

The 40-minute gap before 11:00 starts a new session.

Constraints

  • Return only the requested columns.
  • Use deterministic ORDER BY when multiple rows are returned.
  • Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputevents at 10:00, 10:20, 11:00
Expected[[1,1],[2,0],[3,1]]

The 40-minute gap before 11:00 starts a new session.

Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks