Level 1 Adding pins and ranking them
A photo-sharing site keeps a catalogue of pins. Each pin has a string ID, an integer score (higher is better) and a string type such as "recipe" or "travel". Build PinBoard:
add_pin(pin_id: str, score: int, pin_type: str) -> bool: store the pin and returnTrue. If a pin with this ID already exists, change nothing and returnFalse.top_k(pin_type: str, k: int) -> list[str]: the IDs of thekbest pins of that type, best first. "Best" means higher score; equal scores are ordered by pin ID, smaller string first. If the type has fewer thankpins, return all of them; an unknown type ork == 0gives[].
b = PinBoard()
b.add_pin("p1", 50, "recipe") # True
b.add_pin("p2", 80, "travel") # True
b.add_pin("p3", 70, "recipe") # True
b.add_pin("p0", 50, "recipe") # True
b.add_pin("p3", 99, "travel") # False: p3 exists already
b.top_k("recipe", 2) # ["p3", "p0"] p0 and p1 tie on 50, "p0" < "p1"
b.top_k("recipe", 10) # ["p3", "p0", "p1"]
b.top_k("travel", 1) # ["p2"]
b.top_k("music", 3) # []
Scale: up to 50,000 pins and 20,000 top_k calls mixed together, with k usually small (at most 100). Sorting the whole type (or scanning it with a heap) on every call is too slow when one type holds tens of thousands of pins: keep each type ranked as pins arrive, so a query costs about O(k).