KatInterview

Ad Campaign Event Aggregator with Pluggable Event Types

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designtime-seriesprefix-sums

Last updated

Design an object-oriented component that ingests ad events and returns windowed per-campaign metric sums and ratios, while letting new event types be added without editing core code.

Build a Campaign Metrics Aggregator

You are designing the core of a service that watches a stream of advertising events and answers questions about campaign performance. Events such as clicks, impressions and conversions arrive one at a time. At any moment, a caller may ask for a metric of one campaign over a time window.

What to deliver

Sketch the classes and method signatures for this component, explain how data flows through them, and implement the main parts in code (Python or a language of your choice).

Requirements

  1. Pluggable event types. A developer must be able to introduce a new kind of event (for example video_view or hover) without changing the ingestion or query code that already exists.
  2. Windowed sums. Given a campaign_id and an inclusive time range [start_time, end_time], return the total of a named base metric (such as clicks or spend) for that campaign.
  3. Derived metrics. Support ratio-style metrics such as click-through rate (clicks divided by impressions) and conversion rate over the same window.
  4. Public interface. Define the classes and methods used to record events and to fetch metrics.

Constraints

  • Events are delivered in order of arrival, and you may assume timestamps are mostly non-decreasing.
  • Concentrate on class structure and API design. Durable storage and distributed deployment are out of scope; an in-memory design is fine.
  • The result should be decoupled, readable and easy to maintain.

Example usage

tracker = MetricsTracker()
tracker.record_event(ClickEvent(campaign_id="C1", timestamp=100, cost=0.5))
tracker.record_event(ImpressionEvent(campaign_id="C1", timestamp=101))

total_clicks = tracker.get_metric("C1", "clicks", start=0, end=200)  # 1

Discussion points to cover

  • How the tracker avoids branching on event type.
  • How range queries stay fast as data grows.
  • Where ratio metrics are computed, and why.
  • How you handle empty windows, unknown campaigns, a zero denominator and an out-of-order event.

Hints

Hint 1

Let each event describe its own contribution: give the event base class a method that returns a mapping from base metric names to numeric values. The tracker then only loops over that mapping and never inspects the concrete class.

Hint 2

Keep one time-ordered series per (campaign, metric) pair, storing a running prefix sum. An inclusive range sum becomes two binary searches (left bound for start, right bound for end) and a subtraction.

Hint 3

A ratio such as CTR cannot be added across events or buckets. Store only base sums, and compute derived metrics at query time from windowed sums using a registry of small formula functions. Decide and state what you return when the denominator is zero.