Return the Highest-Bid Ad Campaign Matching a Bid Request's Demographics and Format

Quick Overview

Implement an in-memory ad campaign store with an insert operation and a query that returns the highest-bidding campaign matching a bid request's demographics and ad format. Tests the choice of matching rule, hash-map and priority-queue indexing, tie-breaking and removal of stale campaigns.

Return the Highest-Bid Ad Campaign Matching a Bid Request's Demographics and Format

Company: StackAdapt

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Implement an in-memory store of ad campaigns with two operations: - `insert(campaign) -> None` adds a campaign. - `query(bid_request) -> campaign` returns the campaign with the highest bid price among the campaigns that match the request's demographics and ad format. A campaign carries an id, a bid price, the ad format it serves (for example banner or video) and the demographics it targets. A bid request describes one ad slot: the ad format it accepts and the demographics of the user who will see the ad. The exact shape of a demographic target is not given, so settle it with the questions below and state your matching rule before writing code. ```hint Index by what the query knows A query arrives with an ad format and the user's demographic values and needs one winner. Think about what lookup key those values can form, and which structure under that key keeps the highest bid on top. ``` ```hint Ties and repeats Two campaigns can bid the same price, and the same campaign id may be inserted again with a new bid. Decide what `query` returns in each case before you write it. ``` ### Constraints and Clarifications - Assume each campaign serves one ad format. - `query` returns `None` when no campaign matches. - Bid prices are positive; represent them so that equal bids compare exactly. ### Clarifying Questions - What does a demographic target look like: a single segment, a set of accepted values per dimension (such as age bracket, gender or region), or free-form tags? Are the possible values known in advance? - If a campaign leaves a dimension untargeted, does it match every value of that dimension? - Must the campaign accept every demographic value in the request, or is one overlap enough? - How are equal bids broken? - Does a returned campaign stay eligible, or can campaigns be paused or run out of budget? - Does inserting an existing campaign id replace the earlier version? - Roughly how many campaigns are there, and how often do inserts happen compared with queries? ### What a Strong Answer Covers - An explicit matching rule and an index designed around it. - Insert and query complexity, including what the index costs at insert time and in memory. - Correct winner selection: the highest bid, a deterministic tie-break, and no removed or superseded campaign ever returned. - Edge cases: no match, re-inserted ids, campaigns that target many values, request values outside the known set. ### Follow-up Questions - Campaigns can be paused or run out of budget. How do you remove them without slowing down queries? - One demographic dimension has hundreds of values. What happens to your index, and how would you restructure it? - The ad server wants the top k campaigns for an auction instead of one. What changes? - How would you make inserts and queries safe to run concurrently from many threads?

Overview: Implement an in-memory ad campaign store with an insert operation and a query that returns the highest-bidding campaign matching a bid request's demographics and ad format. Tests the choice of matching rule, hash-map and priority-queue indexing, tie-breaking and removal of stale campaigns.

Read the full StackAdapt Software Engineer interview experience this question came from

StackAdapt logo
StackAdapt
Nov 24, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

Implement an in-memory store of ad campaigns with two operations:

  • insert(campaign) -> None adds a campaign.
  • query(bid_request) -> campaign returns the campaign with the highest bid price among the campaigns that match the request's demographics and ad format.

A campaign carries an id, a bid price, the ad format it serves (for example banner or video) and the demographics it targets. A bid request describes one ad slot: the ad format it accepts and the demographics of the user who will see the ad. The exact shape of a demographic target is not given, so settle it with the questions below and state your matching rule before writing code.

Constraints and Clarifications

  • Assume each campaign serves one ad format.
  • query returns None when no campaign matches.
  • Bid prices are positive; represent them so that equal bids compare exactly.

Clarifying Questions Guidance

  • What does a demographic target look like: a single segment, a set of accepted values per dimension (such as age bracket, gender or region), or free-form tags? Are the possible values known in advance?
  • If a campaign leaves a dimension untargeted, does it match every value of that dimension?
  • Must the campaign accept every demographic value in the request, or is one overlap enough?
  • How are equal bids broken?
  • Does a returned campaign stay eligible, or can campaigns be paused or run out of budget?
  • Does inserting an existing campaign id replace the earlier version?
  • Roughly how many campaigns are there, and how often do inserts happen compared with queries?

What a Strong Answer Covers Guidance

  • An explicit matching rule and an index designed around it.
  • Insert and query complexity, including what the index costs at insert time and in memory.
  • Correct winner selection: the highest bid, a deterministic tie-break, and no removed or superseded campaign ever returned.
  • Edge cases: no match, re-inserted ids, campaigns that target many values, request values outside the known set.

Follow-up Questions Guidance

  • Campaigns can be paused or run out of budget. How do you remove them without slowing down queries?
  • One demographic dimension has hundreds of values. What happens to your index, and how would you restructure it?
  • The ad server wants the top k campaigns for an auction instead of one. What changes?
  • How would you make inserts and queries safe to run concurrently from many threads?