Maximum Concurrent Viewers

Quick Overview

Given a timestamp-sorted stream of viewer join and leave events, find the largest number of viewers watching a broadcast at the same moment, with joins counted before leaves when they share a timestamp. It tests grouping events by key in one linear pass and careful handling of ties.

Maximum Concurrent Viewers

Company: Twitch

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are processing a live stream of viewer events for an online broadcast. Each event is a pair `[timestamp, type]`, where `type` is `1` if a viewer joined the stream at that timestamp and `-1` if a viewer left the stream at that timestamp. Given the array `events` sorted in non-decreasing order of `timestamp`, return the maximum number of concurrent viewers that were watching the stream at any single moment. If multiple events share the same timestamp, all joins at that timestamp are counted before any leaves at the same timestamp, so the peak is measured after simultaneous arrivals. ### Function Signature ```python def max_concurrent_viewers(events: list[list[int]]) -> int: ``` ### Rules - The viewer count starts at 0 before the first event. A join adds 1 and a leave subtracts 1. - Events with the same timestamp can appear in any order inside the array. Apply every join at that timestamp first, read the count, then apply the leaves. - Return the largest count reached at any timestamp. ### Constraints - `1 <= events.length <= 10^5` - `events[i] = [timestamp, type]` - `0 <= timestamp <= 10^9` - `type` is either `1` (join) or `-1` (leave). - Events are given in non-decreasing order of `timestamp`. ### Examples ```text Input: events = [[1,1],[2,1],[3,-1],[4,1],[5,1],[6,-1],[7,-1],[8,-1]] Output: 3 ``` For t = 1 through 8 the viewer count is 1, 2, 1, 2, 3, 2, 1, 0. The peak is 3, reached at t = 5. ```text Input: events = [[10,1],[10,1],[10,1],[10,-1],[20,1]] Output: 3 ``` At t = 10 the three joins are counted first, so the count reaches 3 before the leave brings it down to 2. The join at t = 20 brings it back to 3, which ties the peak.

Overview: Given a timestamp-sorted stream of viewer join and leave events, find the largest number of viewers watching a broadcast at the same moment, with joins counted before leaves when they share a timestamp. It tests grouping events by key in one linear pass and careful handling of ties.

Twitch logo
Twitch
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are processing a live stream of viewer events for an online broadcast. Each event is a pair [timestamp, type], where type is 1 if a viewer joined the stream at that timestamp and -1 if a viewer left the stream at that timestamp.

Given the array events sorted in non-decreasing order of timestamp, return the maximum number of concurrent viewers that were watching the stream at any single moment.

If multiple events share the same timestamp, all joins at that timestamp are counted before any leaves at the same timestamp, so the peak is measured after simultaneous arrivals.

Function Signature

def max_concurrent_viewers(events: list[list[int]]) -> int:

Rules

  • The viewer count starts at 0 before the first event. A join adds 1 and a leave subtracts 1.
  • Events with the same timestamp can appear in any order inside the array. Apply every join at that timestamp first, read the count, then apply the leaves.
  • Return the largest count reached at any timestamp.

Constraints

  • 1 <= events.length <= 10^5
  • events[i] = [timestamp, type]
  • 0 <= timestamp <= 10^9
  • type is either 1 (join) or -1 (leave).
  • Events are given in non-decreasing order of timestamp .

Examples

Input: events = [[1,1],[2,1],[3,-1],[4,1],[5,1],[6,-1],[7,-1],[8,-1]]
Output: 3

For t = 1 through 8 the viewer count is 1, 2, 1, 2, 3, 2, 1, 0. The peak is 3, reached at t = 5.

Input: events = [[10,1],[10,1],[10,1],[10,-1],[20,1]]
Output: 3

At t = 10 the three joins are counted first, so the count reaches 3 before the leave brings it down to 2. The join at t = 20 brings it back to 3, which ties the peak.