KatInterview

Ad Targeting Engine and Route Optimization

Data Structure DesignModel answer
MediumModel answerData Structure Designindexinggraphsoptimization

Last updated

Build an ad matcher with inverted indexes and a delivery router using shortest-path precomputation plus TSP heuristics.

You are building two independent services:

Part A: Ad Targeting Engine

Design and implement a data structure that manages ad campaigns and matches them to incoming requests.

Operations:

  • add(campaign): Register a new campaign or update an existing one
  • remove(campaign_id): Unregister a campaign
  • match(request, k): Return up to k best-matching campaigns

Specifications:

  • Each campaign has: unique ID, targeting rules (attribute → allowed values), bid amount, and remaining budget
  • A request contains attributes (e.g., {"location": "NYC", "device": "mobile", "interests": ["sports", "tech"]})
  • A campaign matches if all its targeted attributes are satisfied by the request. Attributes can be single-valued or multi-valued (sets).
  • Only campaigns with budget >= bid are eligible
  • Return the top k campaigns ranked by bid (break ties by ID)

Optimize for high read throughput (matching happens far more often than campaign updates). Discuss time/space complexity and trade-offs.

Part B: Delivery Route Planner

Given a road network (weighted directed graph with non-negative edges), a depot location, and N delivery stops, compute an efficient round-trip route.

Input:

  • Graph: nodes, edges with weights (distances or travel times)
  • Depot: starting/ending node
  • Stops: list of N nodes to visit

Output:

  • Ordered list of stops forming a route (depot → stops → depot)
  • Total route cost

Compare exact and approximate approaches. Discuss complexity, practical scaling limits, and how to handle real-world constraints like vehicle capacity or time windows.

Hints

Hint 1

For ad matching: Think about reversing the lookup direction—instead of scanning campaigns, index by attribute values

Hint 2

For routing: Precompute pairwise distances first, then solve the ordering problem separately

Hint 3

Consider when exact solutions become impractical and what heuristics give acceptable results