KatInterview

Failure Streaks and Fix Times from a Live Test Log

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designdesignstreamingintervals

Last 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]. status is either "failing" or "passing". The result is None.
  • "get_min_fix_time" with arguments [test_id]. The result is an integer or None.
  • "get_max_concurrent_failure_period" with arguments [min_tests]. The result is a dictionary or None.

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 None if there is no qualifying period, including when min_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 log operations are integers and strictly increasing.
  • status is always "failing" or "passing".
  • test_id values 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.