KatInterview

Log Test Executions and Query Failure Recovery and Concurrent Failures

Data Structure DesignModel answer
MediumModel answerData Structure Designdesign-dsgreedy-intervalsarrays

Last updated

Design an event-driven system to track test statuses, calculate minimum recovery durations, and identify the longest continuous window of high concurrent failures.

You are tasked with building a monitoring service that tracks automated test suite execution logs over time.

Each incoming log entry records a test_id, a numeric timestamp, and a status which is either "PASS" or "FAIL". Timestamps arrive in strictly monotonically increasing order across all operations.

Implement a class TestTracker with the following methods:

  1. log_run(test_id: str, timestamp: int, status: str) -> None:

    • Records an execution log entry.
    • Timestamps are guaranteed to be strictly strictly increasing ($t_i > t_{i-1}$).
    • A test transitions to a failing state upon receiving a "FAIL" log. Consecutive "FAIL" entries for the same test belong to the same continuous failure sequence; the recovery clock starts at the timestamp of the first "FAIL" in this sequence.
    • A test exits the failing state upon receiving a "PASS" log.
  2. get_min_time_to_pass(test_id: str) -> Optional[int]:

    • Computes the minimum time taken for test_id to recover from a failing state to a passing state across all recorded failure-to-pass transitions.
    • The elapsed time for a transition is defined as $t_{\text{pass}} - t_{\text{first_fail}}$.
    • Returns None if the test has never successfully recovered from a failure (or was never logged/failed).
  3. get_longest_failure_window(min_tests: int) -> Optional[Dict[str, int]]:

    • Finds the maximal continuous time interval $[start_timestamp, end_timestamp]$ during which at least min_tests distinct tests were simultaneously in a failing state.
    • A window opens at the timestamp when the count of currently failing tests reaches or exceeds min_tests.
    • A window closes at the timestamp when the count of failing tests drops below min_tests.
    • If a window remains open at the end of all logged events, it implicitly closes at the timestamp of the very last recorded event.
    • If multiple valid windows share the maximum duration ($end_timestamp - start_timestamp$), return the one with the earliest start timestamp.
    • If min_tests is never reached, return None.
    • Returns a dictionary format: {"start_timestamp": start_ts, "end_timestamp": end_ts}.

Hints

Hint 1

For `get_min_time_to_pass`, store state per `test_id`. Remember to track whether an active failure streak is ongoing and record only the timestamp of the *first* failure in that streak.

Hint 2

Since timestamps arrive in strictly increasing order, the global log history is already ordered by time. You do not need to sort events.

Hint 3

For `get_longest_failure_window`, evaluate the state of all tests using a sweep-line technique over the logged events. Maintain a active set of failing test IDs as you iterate through the events.