Implement an in-memory service that chooses which movie or show title to display on a user's homepage billboard.
Each title has a relevance score for the current user. Higher-scored titles should generally be returned first, but the service should avoid returning the same title on two consecutive calls when another title is available.
Support these APIs:
-
upsertTitleScore(title_id: string, score: float)
-
Insert a new title or update the score of an existing title.
-
getTopTitle() -> string
-
Return the title that should be shown now.
Requirements:
-
Titles with higher scores should be preferred.
-
If there is more than one available title, avoid returning the same title on two consecutive
getTopTitle()
calls.
-
A title remains eligible after being returned and may be returned again later.
-
If the system contains only one title, it may be returned repeatedly.
-
upsertTitleScore
may be called many times for the same
title_id
, and updates must take effect correctly.
Design and implement the data structure and explain the time complexity of your operations.