KatInterview

Rolling Ten-Minute Error Counter by Endpoint Prefix

Data Structure DesignAuto-graded · 17 test cases
EasyAuto-gradedData Structure Designdesignbinary-searchhash-map

Last updated

Replay a log of error-recording and alert-check operations, grouping errors by URL prefix and counting those in an inclusive 600-second window against a strict threshold.

You are building a small error-rate monitor for a web service. Errors are grouped by an endpoint prefix, and an alert fires for a prefix when too many errors have occurred recently.

Implement a function that replays a sequence of operations on such a monitor and returns one result per operation.

Operations

Operations are given as two parallel lists: operations[i] is the name of the i-th operation and arguments[i] is the list of its arguments.

  1. "record_error" with arguments [timestamp, url]

    • Logs one error for url at time timestamp (an integer number of seconds).
    • The error is attributed to the prefix of the URL: split the URL on / and keep the first three pieces, joined back with /. This is the host plus the first two path segments. For example, api.com/v1/users/list has prefix api.com/v1/users.
    • If the URL has fewer than three pieces after splitting, the whole split result is kept, so the prefix is the URL itself. A trailing slash yields an empty final piece, so api.com/v1/users/ has prefix api.com/v1/users.
    • The result for this operation is None.
  2. "should_alert" with arguments [timestamp, prefix, threshold]

    • Counts the errors recorded so far whose attributed prefix is exactly equal to prefix (the given string is used as is, not re-normalized) and whose error time s satisfies timestamp - 600 <= s <= timestamp. Both ends of the window are inclusive.
    • The result is True if this count is strictly greater than threshold, otherwise False.
    • A prefix that has never had an error simply has a count of 0.

Any other operation name must raise a ValueError.

Details to get right

  • Several errors may share the same timestamp, and each one counts separately.
  • Errors may be recorded in any time order, and a check may use a timestamp earlier than some recorded errors. Only errors inside the window [timestamp - 600, timestamp] count, so errors later than timestamp are ignored, as are errors older than timestamp - 600.
  • A check never removes or alters stored errors; it only reads them.

Return value

A list with one entry per operation, in order: None for each "record_error", and a boolean for each "should_alert".

Constraints

  • len(operations) == len(arguments).
  • Timestamps are positive integers.
  • threshold is a non-negative integer.
  • The number of operations can be large, so each operation should be fast (avoid rescanning every recorded error on each check).

Examples

Example 1

Input: replay_error_monitor(['record_error', 'record_error', 'should_alert', 'should_alert'], [[1000, 'api.com/v1/users/login'], [1200, 'api.com/v1/users/create'], [1201, 'api.com/v1/users', 1], [1201, 'api.com/v1/users', 2]])

Output: [None, None, True, False]

Hints

Hint 1

Compute the prefix once when an error is recorded, and store timestamps in a dictionary keyed by that prefix.

Hint 2

Keep each prefix's timestamps in a sorted list. Counting those in a range then needs only two binary searches rather than a full scan.

Hint 3

The window [t - 600, t] is inclusive on both sides: count = (number of values <= t) - (number of values < t - 600). Use bisect_right and bisect_left, and compare the count with the threshold using strict >.