Netflix's trending algorithm needs to detect titles whose hourly view count
exceeds 2x their trailing 24-hour average at any point.
You are given a stream of events as a list of tuples:
events: List[Tuple[int, str]] # (unix_timestamp, title_id)
Implement:
def find_trending_titles(events: List[Tuple[int, str]], window_hours: int = 24, spike_multiplier: float = 2.0) -> List[Tuple[str, int]]:
Returns a list of (title_id, spike_timestamp) for every title that spiked above spike_multiplier * its trailing window_hours average at any hourly boundary.
Events are NOT guaranteed to be sorted by timestamp. A title can appear multiple times if it spikes more than once. Return the list sorted by title_id, then by spike_timestamp (both ascending).
Constraints:
- Events list can contain up to 10 million entries
- Timestamps span up to 30 days
- Optimize for O(n log n) or better
- Use only standard library (collections, heapq, bisect allowed)