Part 1: Basic Ad Server
Design an AdServer class with two APIs:
InsertAd(content, score): Insert a new advertisement with a score.GetAd(): Return the best eligible advertisement.
Requirements:
- Always select the ad with the highest score.
- Never return the same ad twice consecutively.
- After an ad is served, decrease its score by 1.
Part 2: Ad Cooldown / Delay
Extend the existing implementation to support:
InsertAd(content, score, delay)GetAd()remains unchanged.
Additional requirements:
delayspecifies how many other ads must be served before the same ad can be served again.- Each ad can have a different delay.
This problem asks you to design an ad-serving system that repeatedly returns the highest-scoring eligible ad while preventing the same ad from being served consecutively and decreasing its score after each serving. In the extended version, each ad also has its own cooldown delay, meaning it can only become eligible again after a certain number of other ads have been served. A good solution typically uses a max-heap for currently eligible ads plus another structure to track ads waiting for cooldown. Careful handling of score decay, tie-breaking, and re-entry into the eligible pool is the key challenge.