← Questions
Ads retrieval service using a heap
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Mar 2026. It tests heaps and design.
Build a small ads retrieval service that keeps a pool of ads, each with a score (for example predicted value), and returns the best k ads for a request, ranked by score. Ads are added, removed and re-scored while the service runs. Use a heap, and discuss the cost of each operation.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
add_ad(ad_id: str, score: float) -> None— adds an ad, or updates its score if it exists. -
remove_ad(ad_id: str) -> None— removes it; unknown ids are ignored. -
top_k(k: int) -> list[str]— the ids of thekhighest-scoring live ads, best first (ties by id). Fewer if the pool is smaller. Does not remove them.
Example
add_ad("ad1", 0.4); add_ad("ad2", 0.9); add_ad("ad3", 0.7)
top_k(2) -> ["ad2", "ad3"]
remove_ad("ad2"); add_ad("ad4", 0.7)
top_k(2) -> ["ad3", "ad4"] # tie at 0.7, broken by id
add_ad("ad1", 0.95) # re-score
top_k(3) -> ["ad1", "ad3", "ad4"]
Constraints
- Up to 10^6 ads; up to 10^5 operations per second;
kis small (around 10).
Clarifying questions
- Is
top_kread-only, or does serving an ad remove it (budget used up)? - How often do scores change compared with how often we query?
- Does a request filter ads (by keyword, region, budget left), or is it one global pool?
- Do we need exact results, or is slightly stale ranking acceptable?
What a strong answer covers
- A max-heap of
(score, ad_id)plus a map of current scores. Removes and re-scores use lazy deletion: push the new entry and skip stale heap entries when they reach the top. -
top_k: pop untilklive entries are found, then push them back:O((k + stale) log n). Add and update areO(log n). - Periodic rebuild when stale entries pile up, so the heap does not grow without bound.
- Alternatives: an indexed heap that supports decrease-key in
O(log n); a balanced tree or sorted set withO(log n)updates andO(k)reads, often better here. - Per-request filters: a heap per keyword or segment, or retrieve candidates by filter first and rank them with a size-
kmin-heap. - Tests: ties, removing the top ad, re-scoring up and down,
kbigger than the pool.
Follow-ups
- Each ad has a budget; serving it spends budget and an empty ad must disappear.
- Rank by
bid * predicted_ctr, where the click-through prediction depends on the request. - Spread the pool over several machines and merge their top
k.
Where it's asked
- Google · Onsite coding · Last asked Mar 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
