This question evaluates understanding of priority queues/heaps, object-oriented API design, and algorithmic techniques for maintaining dynamic priorities such as add, remove, and update operations.
Design a component that manages ads and supports retrieving the “best” ads based on a score.
Implement APIs such as:
addAd(adId, score, metadata) -> void
removeAd(adId) -> bool
updateScore(adId, newScore) -> bool
getTopAd() -> Ad | null
(or
getTopK(k) -> List<Ad>
)
Assume higher score means higher priority.
updateScore
efficiently given that typical heaps don’t support decrease/increase-key by ID?
getTopAd()
doesn’t return ads that were removed or superseded by a newer score?