KatInterview

Real-Time Log Event Aggregation in React

Data Structure DesignModel answer
MediumModel answerData Structure Designreactdata-structuresstreaming

Last updated

Design and build a React component that ingests a real-time event stream, aggregates stats per category, handles out-of-order timestamps, and throttles re-renders for high throughput.

You are tasked with implementing a front-end dashboard component in React that processes an incoming stream of structured log events and displays live aggregated statistics.

Event Format

Every event emitted by the stream has the following structure:

  • category: A string indicating the source system (e.g., "auth", "checkout").
  • timestamp: A string formatted as an ISO-8601 date (e.g., "2025-10-24T14:32:00Z") or an epoch integer in milliseconds (e.g., 1761316320000).

Example event:

{ "category": "auth", "timestamp": "2025-10-24T14:32:00Z" }

Functional Requirements

  1. Stream Consumption: Create a component (or custom React hook) that accepts a subscription callback (e.g., subscribe(onEvent)).
  2. Aggregation: Store and update statistics grouped by category:
    • Frequency: The total event count for that category.
    • Last Seen: The most recent event timestamp encountered for that category.
  3. UI Display: Render a sorted table or list updating dynamically as new logs arrive.
  4. Empty State: Show a user-friendly message when no events have been recorded yet.

Performance & Data Integrity Constraints

  • Logs may arrive out-of-order due to network latency; "Last Seen" must represent the maximum logical timestamp observed for that category, not simply the arrival order.
  • The stream can transmit hundreds or thousands of events per second. Your solution must prevent high-frequency re-renders from degrading UI performance.
  • Malformed timestamps should not crash the application; malformed events missing a category should be gracefully ignored.

Hints

Hint 1

How can you avoid copying a Map on every single incoming event while ensuring React re-renders when data changes?

Hint 2

Consider buffering incoming events over small time windows (such as animation frames) rather than triggering state updates per event.

Hint 3

What strategy guarantees that out-of-order log timestamps do not overwrite newer timestamps with older values?