Deduplicate Retried Pipeline Runs by Idempotency Key
Problem Statement
A pipeline trigger may fire multiple times for the same commit. Count the distinct runs actually executed when duplicate triggers (same idempotency key within a time window) are collapsed.
Background
Each trigger has (key, time). A trigger starts a new run only if no run with the same key started within the last window seconds; otherwise it is a duplicate and reuses the existing run. Process triggers in time order and count distinct runs started.
Your Task
def distinct_runs(triggers, window):
- triggers: list of (key, time), time non-decreasing.
- Return the count of runs actually started.
Input Format
- triggers (list of (str, int)), window (int).
Output Format
- A single int.
Sample
print(distinct_runs([("c1", 0), ("c1", 5), ("c1", 100)], 60))
Output:
2
Example
print(distinct_runs([("c1", 0), ("c1", 5), ("c1", 100)], 60))2
- Process the first trigger
("c1", 0): Since no previous run exists for keyc1, a new run is started. The count increases to 1, and the last run time forc1is recorded as 0. - Process the second trigger
("c1", 5): Check the time difference from the last run: . Since (the window), this trigger is a duplicate and does not start a new run. The count remains 1. - Process the third trigger
("c1", 100): Check the time difference from the last recorded run (which is still at time 0): . Since , the window has expired, so a new run is started. The count increases to 2, and the last run time forc1is updated to 100. - The final output is 2
Constraints
- A trigger starts a run unless a run with the same key started within
windowseconds before it. - Update the key's last-run time whenever a new run starts.
- Return the number of runs started.