Failure Streaks and Fix Times from a Live Test Log
Data Structure DesignAuto-graded · 18 test casesLast updated
Process a stream of pass/fail log operations, answering shortest-fix-time and longest-concurrent-failure-window queries with an incremental, level-based data structure.
You are given a recorded session of operations against a test-result tracker. Process the operations in order and return one result per operation.
Each operation is a name in operations, with its arguments at the same index in arguments:
"log"with arguments[test_id, timestamp, status].statusis either"failing"or"passing". The result isNone."get_min_fix_time"with arguments[test_id]. The result is an integer orNone."get_max_concurrent_failure_period"with arguments[min_tests]. The result is a dictionary orNone.
Timestamps are integers and strictly increase across all log operations in the session.
Failure episodes
- A test begins a failure episode at its first
"failing"log after being non-failing. A test is non-failing initially and after a"passing"log. - A
"failing"log for a test that is already failing changes nothing. The episode still began at the earlier log. - A
"passing"log for a test that is not failing changes nothing. - A
"passing"log for a failing test ends its episode. The test is failing on the half-open interval[fail_start, pass_timestamp). - The fix time of a resolved episode is
pass_timestamp - fail_start.
Queries
get_min_fix_time(test_id): return the smallest fix time over all resolved episodes of that test so far. Return None if the test is unknown or has no resolved episode. An episode that is still failing is ignored.
get_max_concurrent_failure_period(min_tests): consider only the logs processed so far. A qualifying period is a maximal time interval [start, end) in which at least min_tests distinct tests are failing at once, and which has ended, meaning the number of failing tests drops below min_tests at end. Return the qualifying period with the greatest length end - start as {"start_timestamp": start, "end_timestamp": end}.
- If several qualifying periods have the same greatest length, return the one that starts earliest.
- A period that is still ongoing, because the failing count has not dropped below
min_tests, is not counted. - Return
Noneif there is no qualifying period, including whenmin_tests < 1.
Return the list of all results, in operation order.
Constraints
len(operations) == len(arguments), and the session may be empty of logs.- Timestamps in
logoperations are integers and strictly increasing. statusis always"failing"or"passing".test_idvalues are strings.- Queries may appear at any point, including before any log.
- Aim for each operation to run in roughly constant time, without rescanning the full history for every query.
Examples
Example 1
Input: run_test_log_session(['log', 'log', 'get_min_fix_time', 'get_max_concurrent_failure_period', 'log', 'get_max_concurrent_failure_period'], [['a', 1, 'failing'], ['a', 5, 'passing'], ['a'], [1], ['b', 7, 'failing'], [1]])
Output: [None, None, 4, {'start_timestamp': 1, 'end_timestamp': 5}, None, {'start_timestamp': 1, 'end_timestamp': 5}]
Example 2
Input: run_test_log_session(['log', 'log', 'log', 'log', 'get_min_fix_time', 'get_max_concurrent_failure_period'], [['a', 1, 'failing'], ['a', 3, 'failing'], ['a', 8, 'passing'], ['b', 9, 'failing'], ['a'], [1]])
Output: [None, None, None, None, 7, {'start_timestamp': 1, 'end_timestamp': 8}]
Example 3
Input: run_test_log_session(['get_max_concurrent_failure_period'], [[1]])
Output: [None]
Hints
Hint 1
Each log changes the number of simultaneously failing tests by at most one. Think about what that means for how a 'at least k failing' period can begin and end.
Hint 2
For each level k, remember when the failing count most recently rose to k (the open period's start), and the longest period for level k that has already closed.
Hint 3
When a failing test passes, the period for the current top level closes. Update that level's best only if the new period is strictly longer, which keeps the earliest on ties. Track per-test failure start times in a dictionary for fix times.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Fix times. Keep a dictionary fail_start holding the start timestamp of each test's current failure episode. A "failing" log only creates an entry if none exists, so consecutive failures merge into one episode that starts at the first failure. A "passing" log removes the entry if one exists. It computes ts - start and updates that test's minimum in min_fix. A passing log for a non-failing test does nothing. The query is a dictionary lookup, which returns None if no episode has been resolved.
Concurrent failure periods. Let count be the number of currently failing tests. Each log changes count by 0 or exactly ±1. So a maximal period with count >= k starts exactly when count rises from k-1 to k, and ends exactly when it falls from k to k-1. The count cannot skip a level.
For every level k we store:
level_start[k]: whencountlast rose tok. This is the start of the open>= kperiod.best[k]: the longest closed>= kperiod so far, as(start, end).
When a test starts failing, count increases to c and we set level_start[c] = ts. When a failing test passes, the top-level period [level_start[count], ts) closes. We replace best[count] only if the new period is strictly longer, which keeps the earliest period on ties. Then count decreases. Lower levels remain open, as they should.
A query for min_tests = k reads best[k]. It returns None if k < 1, if level k was never reached, or if no period at that level has closed yet. Open periods are never reported, as the task requires.
Why it is correct
- Each episode is opened once and closed once, so every resolved episode contributes exactly its duration to
min_fix. - By the ±1 argument, closed maximal
>= kperiods correspond one to one withk-1 -> kfollowed byk -> k-1transitions, and every such period is compared againstbest[k]when it closes. - Periods close in chronological order, and
best[k]is replaced only on a strictly longer period. So among equal lengths the earliest wins. - Timestamps strictly increase, so every closed period has positive length.
Complexity
log: O(1) amortized, using dictionary operations and an occasional list append.get_min_fix_time: O(1).get_max_concurrent_failure_period: O(1).- Space: O(T + M), where T is the number of distinct tests and M is the peak number of simultaneously failing tests. No history is stored.
Reference solution
from typing import List
def run_test_log_session(operations: List[str], arguments: List[List]) -> List:
fail_start = {} # test_id -> start of its current failure episode
min_fix = {} # test_id -> smallest resolved fix time
count = 0 # number of tests failing right now
level_start = [None] # level_start[k]: start of open ">= k failing" period
best = [None] # best[k]: longest closed ">= k failing" period (start, end)
results = []
for op, args in zip(operations, arguments):
if op == "log":
test_id, ts, status = args
if status == "failing":
if test_id not in fail_start:
fail_start[test_id] = ts
count += 1
if count == len(level_start):
level_start.append(None)
best.append(None)
level_start[count] = ts
else:
start = fail_start.pop(test_id, None)
if start is not None:
duration = ts - start
prev = min_fix.get(test_id)
if prev is None or duration < prev:
min_fix[test_id] = duration
s = level_start[count]
level_start[count] = None
cur = best[count]
if cur is None or ts - s > cur[1] - cur[0]:
best[count] = (s, ts)
count -= 1
results.append(None)
elif op == "get_min_fix_time":
results.append(min_fix.get(args[0]))
else: # get_max_concurrent_failure_period
k = args[0]
if 1 <= k < len(best) and best[k] is not None:
s, e = best[k]
results.append({"start_timestamp": s, "end_timestamp": e})
else:
results.append(None)
return results