PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • easy
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

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.

Input: [('processEvent', 'u1', 'c1', 10), ('processEvent', 'u1', 'c2', 20), ('processEvent', 'u2', 'c1', 30), ('processEvent', 'u1', 'c1', 1000), ('getCount', 'u1', 'c1'), ('getCount', 'u1', 'c2'), ('getCount', 'u2', 'c1'), ('getCount', 'u2', 'c2')]

Expected Output: [1, 1, 1, 0]

Explanation: Pairs are tracked independently. For ('u1', 'c1'), the latest timestamp is 1000, so its window is [100, 1000] and the earlier event at 10 is excluded.

Input: [('processEvent', 'u1', 'c1', 1), ('processEvent', 'u1', 'c1', 100), ('processEvent', 'u1', 'c1', 901), ('processEvent', 'u1', 'c1', 1801), ('getCount', 'u1', 'c1'), ('getCount', 'u1', 'c1')]

Expected Output: [2, 2]

Explanation: After the latest event at 1801, the window is [901, 1801]. Only timestamps 901 and 1801 remain in range. Repeated getCount returns the same value until a new event arrives.

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

Expected Output: [4]

Explanation: Duplicate timestamps are allowed. With latest timestamp 1000, the inclusive window is [100, 1000], so all four events are counted.

Input: []

Expected Output: []

Explanation: There are no operations, so there are no getCount results to return.

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.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Distributed Rate Limiter - OpenAI (medium)
  • Compute Plant Infection Time - OpenAI (medium)
  • Implement IP Address Arithmetic - OpenAI (hard)
  • Simulate Infection Spread on a Grid - OpenAI (hard)
  • Implement Social Follow Recommendations - OpenAI (medium)