Uber's demand signal (ride requests per minute per city) tells you when to trigger surge pricing
and alert operations teams. Unusual spikes can indicate special events (concert, stadium, airport delays)
or system issues (app crash, outage).
You are given a stream of demand events as a list of tuples:
events: List[Tuple[int, str]] # (unix_timestamp, city_code)
Implement:
def detect_demand_anomalies(events: List[Tuple[int, str]], window_minutes: int = 60, anomaly_threshold: float = 1.5) -> List[Tuple[str, int]]:
"""
Returns a list of (city_code, anomaly_timestamp) for every city that
spiked above anomaly_threshold * its trailing window_minutes average
at any minute boundary.
Text
Events are NOT guaranteed to be sorted by timestamp.
A city can appear multiple times if it spikes more than once.
"""
Constraints:
- Events list can contain up to 10 million entries
- Timestamps span up to 7 days
- Optimize for O(n log n) or better
- Use only standard library (collections, heapq, bisect allowed)