Databricks' performance monitoring system needs to detect queries that exceed a slow-query threshold
within a rolling window of the last 100 queries per cluster.
You are given a stream of query execution events as a list of tuples:
query_history: List[Tuple[str, str, int]] # (query_id, cluster_id, execution_time_ms)
Implement:
def detect_query_performance_anomalies(query_history: List[Tuple[str, str, int]], slow_threshold_ms: int = 1000) -> List[Tuple[str, str, int]]:
"""
Returns a list of (cluster_id, query_id, execution_time_ms) for every query
that exceeds slow_threshold_ms within its cluster's rolling 100-query window.
Text
Results should be ordered by (cluster_id, query_id).
A cluster can have multiple anomalies if multiple queries exceed the threshold.
"""
Constraints:
- Query history can contain up to 1 million entries
- Optimize for O(n) time complexity (single pass through events)
- Use only standard library (collections allowed)
- The rolling window is per-cluster; do NOT use the global max window