PIXELBANKv9.1.0
Menu

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:

Input:
print(distinct_runs([("c1", 0), ("c1", 5), ("c1", 100)], 60))
Output:
2
Reasoning:
  • Process the first trigger ("c1", 0): Since no previous run exists for key c1, a new run is started. The count increases to 1, and the last run time for c1 is recorded as 0.
  • Process the second trigger ("c1", 5): Check the time difference from the last run: 5−0=55 - 0 = 5. Since 5<605 < 60 (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): 100−0=100100 - 0 = 100. Since 100≥60100 \ge 60, the window has expired, so a new run is started. The count increases to 2, and the last run time for c1 is updated to 100.
  • The final output is 2

Constraints:

  • A trigger starts a run unless a run with the same key started within window seconds before it.
  • Update the key's last-run time whenever a new run starts.
  • Return the number of runs started.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.