Quick Overview

This question evaluates data structures and algorithms competency, focusing on designing efficient in-memory counters and time-windowed event aggregation for (userId, chatId) pairs; it is categorized under Coding & Algorithms and has a practical implementation focus.

Implement Chat Event Counter

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Design an in-memory counter for chat activity. You need to implement a class with the following two methods: ```text processEvent(userId: string, chatId: string, timestamp: int) -> void getCount(userId: string, chatId: string) -> int ``` Each call to `processEvent` records one chat event for the given `(userId, chatId)` at `timestamp`. Assume `timestamp` is a Unix timestamp in seconds. `getCount(userId, chatId)` should return the number of events for that exact `(userId, chatId)` whose timestamps are within the 15-minute window ending at that pair's most recent event timestamp. In other words, if the latest event timestamp recorded for `(userId, chatId)` is `T`, return the number of events with timestamps in the range `[T - 15 * 60, T]`. If no events have been recorded for that pair, return `0`. Example: ```text processEvent("u1", "c1", 100) processEvent("u1", "c1", 200) processEvent("u1", "c1", 1000) getCount("u1", "c1") -> 2 ``` Explanation: the latest timestamp is `1000`, so the 15-minute window is `[100, 1000]`; all three events are in range if the interval is inclusive. If the intended interpretation is strictly after `T - 900`, clarify the boundary with the interviewer. Design the data structures and implement both methods efficiently. Consider that there may be many users and many chats, and that repeated calls should avoid scanning all historical events.

Quick Answer: This question evaluates data structures and algorithms competency, focusing on designing efficient in-memory counters and time-windowed event aggregation for (userId, chatId) pairs; it is categorized under Coding & Algorithms and has a practical implementation focus.

Design an in-memory counter for chat activity. Each chat event belongs to an exact `(userId, chatId)` pair. - `processEvent(userId, chatId, timestamp)` records one event. - `getCount(userId, chatId)` returns how many events for that exact pair fall inside the inclusive 15-minute window ending at that pair's most recent event timestamp. If the latest timestamp recorded for `(userId, chatId)` is `T`, then `getCount(userId, chatId)` should count events with timestamps in `[T - 900, T]`. If no events have ever been recorded for that pair, return `0`. For this coding problem, you will simulate the class using a single function: - Each operation is either `('processEvent', userId, chatId, timestamp)` or `('getCount', userId, chatId)`. - Return a list containing the results of all `getCount` operations in order. Important assumption for efficiency: for the same `(userId, chatId)` pair, `processEvent` calls arrive in non-decreasing timestamp order.

Constraints

  • 0 <= len(operations) <= 2 * 10^5
  • 0 <= timestamp <= 10^9
  • userId and chatId are non-empty strings
  • For the same (userId, chatId) pair, processEvent timestamps are non-decreasing
  • The 15-minute window is inclusive: [T - 900, T]

Examples

Input: [('processEvent', 'u1', 'c1', 100), ('processEvent', 'u1', 'c1', 200), ('processEvent', 'u1', 'c1', 1000), ('getCount', 'u1', 'c1')]

Expected Output: [3]

Explanation: The latest timestamp for ('u1', 'c1') is 1000, so the window is [100, 1000]. Events at 100, 200, and 1000 are all included.

Input: [('getCount', 'u1', 'c1')]

Expected Output: [0]

Explanation: No events were recorded for this pair, so the count is 0.

Hints

  1. Track each (userId, chatId) pair independently. A hash map keyed by the pair is a good starting point.
  2. Because timestamps for the same pair never decrease, a deque can store only the events still inside that pair's latest 15-minute window.

Loading coding console...