KatInterview

Real-time A/B test monitor with late arrival tolerance

System DesignModel answer
MediumModel answerSystem Designstreamingexperimentationstatistics

Last updated

Build a stream processor that detects traffic imbalances across experiment variants in real time, handling duplicate events and delayed data.

You're building a real-time monitor for A/B experiments that processes two event streams:

  • Assignments: (experiment_id, user_id, variant, timestamp) — when a user enters an experiment
  • Pageviews: (user_id, timestamp) — user activity events

Your system must:

  1. Ensure each user counts once per experiment — handle duplicates and replay without double-counting
  2. Track variant populations efficiently — maintain running totals per variant without storing individual events, supporting arrivals up to 24 hours late
  3. Alert on imbalances every minute — compute a chi-square test against the expected traffic split (e.g., 50/50 or 33/33/33), raising an alert when p < 0.0001 and the largest variant share deviates by ≥0.3 percentage points from target
  4. Filter automated traffic — exclude users generating more than N assignments per minute
  5. Scale horizontally — describe how to partition work across multiple workers
  6. Enable offline validation — explain how you'd test the detector against historical data without biasing results

Provide:

  • Data structures and state management approach
  • Pseudocode for the core logic
  • Time and space complexity
  • Partitioning strategy
  • Validation methodology

Assume millions of concurrent experiments and billions of events per day.

Hints

Hint 1

Each layer has different memory constraints: deduplication scales with unique keys, aggregation stays constant per experiment, evaluation runs only on changed experiments

Hint 2

Late events should update the time bucket matching their event timestamp, not their arrival time — this requires a sliding window of buckets

Hint 3

When a user exceeds the rate limit, you can subtract their prior contributions instead of waiting to decide whether to count them