Katkat

Implement Chat Event Counter

OpenAISoftware Engineer · Technical Screen

Last updated

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.

You need to implement a class with the following two methods:

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:

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.