Aggregate per-type statistics from an event stream, skipping duplicate events

Quick Overview

Process a list of timestamped events and return, for each event type, the count, sum, minimum, maximum and latest timestamp, while skipping duplicates that share a timestamp and type. It tests hash map aggregation, set-based deduplication, out-of-order input and deterministic output ordering.

Aggregate per-type statistics from an event stream, skipping duplicate events

Company: Plaid

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of events from an ingestion stream. Each event is a list `[timestamp, event_type, value]`, where `timestamp` is an integer, `event_type` is a string, and `value` is an integer. Process the stream and return aggregated statistics for each `event_type`. For each event type, compute the count of events, the total sum of values, the minimum value, the maximum value, and the latest timestamp. Duplicate events (same `timestamp` and `event_type`) are counted only once, keeping the first occurrence. Events may arrive out of order by timestamp. Return the result as a list of lists sorted alphabetically by `event_type`, where each inner list is `[event_type, count, total, min_val, max_val, last_timestamp]`. ### Function Signature ```python def process_events(events: list[list]) -> list[list]: ``` ### Rules - Two events are duplicates when they have the same `timestamp` and the same `event_type`, wherever they appear in the list. Only the first one in list order is used. Every later duplicate is ignored entirely, even if its `value` differs. - `count`, `total`, `min_val` and `max_val` are computed over the kept events of that type only. - `last_timestamp` is the largest timestamp among the kept events of that type, which is not necessarily the timestamp of the last one to arrive. - Rows are sorted by `event_type` in ascending order of character codes (Python's default string order), so `_` sorts before any lowercase letter. ### Constraints - `1 <= len(events) <= 10^5` - `1 <= timestamp <= 10^9` - `event_type` consists of lowercase English letters and underscores, with `1 <= len(event_type) <= 20` - `-10^6 <= value <= 10^6` - Each event is `[timestamp, event_type, value]` ### Examples ```text Input: events = [[1, "click", 5], [2, "click", 3], [1, "view", 10], [3, "click", 7], [2, "view", 4], [1, "click", 99]] Output: [["click", 3, 15, 3, 7, 3], ["view", 2, 14, 4, 10, 2]] ``` The event `[1, "click", 99]` has the same timestamp and type as `[1, "click", 5]`, so it is skipped. That leaves click values 5, 3 and 7 (count 3, total 15, min 3, max 7, latest timestamp 3) and view values 10 and 4 (count 2, total 14, min 4, max 10, latest timestamp 2). ```text Input: events = [[100, "purchase", -50], [50, "purchase", 200], [100, "purchase", 999]] Output: [["purchase", 2, 150, -50, 200, 100]] ``` The third event duplicates the first and is skipped, so the kept values are -50 and 200. The latest timestamp is 100 even though the event at time 50 arrived after it. ```text Input: events = [[5, "page_view", 1], [3, "pageview", 2], [5, "page_view", 8], [4, "page_view", -3]] Output: [["page_view", 2, -2, -3, 1, 5], ["pageview", 1, 2, 2, 2, 3]] ``` `[5, "page_view", 8]` duplicates the first event, so `page_view` keeps the values 1 and -3. `"page_view"` comes before `"pageview"` because `_` has a smaller character code than `v`.

Overview: Process a list of timestamped events and return, for each event type, the count, sum, minimum, maximum and latest timestamp, while skipping duplicates that share a timestamp and type. It tests hash map aggregation, set-based deduplication, out-of-order input and deterministic output ordering.

Plaid logo
Plaid
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
3
0

You are given a list of events from an ingestion stream. Each event is a list [timestamp, event_type, value], where timestamp is an integer, event_type is a string, and value is an integer.

Process the stream and return aggregated statistics for each event_type. For each event type, compute the count of events, the total sum of values, the minimum value, the maximum value, and the latest timestamp.

Duplicate events (same timestamp and event_type) are counted only once, keeping the first occurrence. Events may arrive out of order by timestamp.

Return the result as a list of lists sorted alphabetically by event_type, where each inner list is [event_type, count, total, min_val, max_val, last_timestamp].

Function Signature

def process_events(events: list[list]) -> list[list]:

Rules

  • Two events are duplicates when they have the same timestamp and the same event_type , wherever they appear in the list. Only the first one in list order is used. Every later duplicate is ignored entirely, even if its value differs.
  • count , total , min_val and max_val are computed over the kept events of that type only.
  • last_timestamp is the largest timestamp among the kept events of that type, which is not necessarily the timestamp of the last one to arrive.
  • Rows are sorted by event_type in ascending order of character codes (Python's default string order), so _ sorts before any lowercase letter.

Constraints

  • 1 <= len(events) <= 10^5
  • 1 <= timestamp <= 10^9
  • event_type consists of lowercase English letters and underscores, with 1 <= len(event_type) <= 20
  • -10^6 <= value <= 10^6
  • Each event is [timestamp, event_type, value]

Examples

Input: events = [[1, "click", 5], [2, "click", 3], [1, "view", 10], [3, "click", 7], [2, "view", 4], [1, "click", 99]]
Output: [["click", 3, 15, 3, 7, 3], ["view", 2, 14, 4, 10, 2]]

The event [1, "click", 99] has the same timestamp and type as [1, "click", 5], so it is skipped. That leaves click values 5, 3 and 7 (count 3, total 15, min 3, max 7, latest timestamp 3) and view values 10 and 4 (count 2, total 14, min 4, max 10, latest timestamp 2).

Input: events = [[100, "purchase", -50], [50, "purchase", 200], [100, "purchase", 999]]
Output: [["purchase", 2, 150, -50, 200, 100]]

The third event duplicates the first and is skipped, so the kept values are -50 and 200. The latest timestamp is 100 even though the event at time 50 arrived after it.

Input: events = [[5, "page_view", 1], [3, "pageview", 2], [5, "page_view", 8], [4, "page_view", -3]]
Output: [["page_view", 2, -2, -3, 1, 5], ["pageview", 1, 2, 2, 2, 3]]

[5, "page_view", 8] duplicates the first event, so page_view keeps the values 1 and -3. "page_view" comes before "pageview" because _ has a smaller character code than v.