Peak Concurrent Viewers per Stream From Session Logs

Last updated: November 29, 2026

Quick Overview

Given a table `viewer_sessions(stream_id, user_id, start_ts, end_ts)` where each row is one viewing session, write SQL that returns each stream's peak concurrent viewer count and the first time that peak was reached.

Twitch
Data Manipulation (SQL/Python)
Data Engineer
Twitch
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given a table `viewer_sessions(stream_id, user_id, start_ts, end_ts)` where each row is one viewing session, write SQL that returns each stream's peak concurrent viewer count and the first time that peak was reached.

Peak concurrent viewers is a headline metric for Twitch creators, sponsors and partner programs, so the definition has to be exact. The interviewer checks that you know the event sweep pattern in SQL, handle sessions that end and start at the same instant, and think about data quality in watch logs.

What the Interviewer Expects
  • Unpivots sessions into +1 and -1 events
  • Uses a running window sum ordered by time
  • Orders ends before starts at equal timestamps and explains why
  • Picks the first peak with ROW_NUMBER
  • Mentions deduplication of overlapping sessions for one user
Key Topics to Cover
Window functions
Event sweep
Running sum
Tie-breaking
Data quality
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • One user has two tabs open on the same stream. Should they count twice?
  • How would you compute this per minute for a concurrency chart instead of only the peak?
  • Sessions with a null end_ts are still open. How do you handle them?
  • How would this run on a 10 billion row table?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

One row per session, timestamps in seconds or as timestamps, end_ts exclusive. Assume closed sessions; open ones (null end) would be capped at the stream end time. Ask whether to dedupe by user: for...

Approach

Turn every session into two events: +1 at start and -1 at end. A running sum of deltas ordered by time gives the concurrency after each event. Because end is exclusive, at a shared timestamp the -1 mu...


Submit Your Answer
Markdown supported

Related Questions