SystemDrills

Session: Sign in to solve

Solution.txt

Exception Threshold Monitor

Problem

Design ExceptionThresholdMonitor to track recent service exceptions under one configured rolling-window policy for each independent exception category. Calls are sequential on one shared instance.

Requirements

  • Use each category's policy. Both public methods use the named category's supplied threshold and windowSeconds.
  • Record the named observation. recordException adds exactly one observation at occurredAtSecond to the named configured category.
  • Preserve other categories when recording. A successful recordException changes no other category's observations or latest successful time.
  • Count only the named category. getRecentCount forms its result only from observations recorded for the named configured category.
  • Exclude the lower boundary. At time t, an observation at exactly t - windowSeconds is outside the rolling window.
  • Include the upper boundary. At time t, an observation at exactly t is inside the rolling window.
  • Expire during count calls. getRecentCount removes expired observations for its asOfSecond even when no exception is recorded at that time.
  • Evaluate after insertion. recordException evaluates the recent count after adding the new observation.
  • Require strict exceedance. recordException returns true exactly when the post-insert count is greater than the named category's threshold. Equality returns false.
  • Reject an unknown record category. recordException raises UnknownCategoryError when the category has no configured policy.
  • Reject an unknown count category. getRecentCount raises UnknownCategoryError when the category has no configured policy.
  • Keep unknown-category rejection atomic. Either error changes no configured category's observations or latest successful time.
  • Keep recording amortized constant time. recordException runs in O(1)O(1) amortized time over a testcase operation sequence.
  • Keep counting amortized constant time. getRecentCount runs in O(1)O(1) amortized time over a testcase operation sequence.

API

Each policy object contains category: string, threshold: integer, and windowSeconds: integer.

SignatureReturnsBehavior
ExceptionThresholdMonitor(policies: object[])Not applicableCreates one empty observation history for every supplied unique category policy.
recordException(category: string, occurredAtSecond: integer)booleanRecords one configured-category occurrence and returns its post-insert strict threshold condition.
getRecentCount(category: string, asOfSecond: integer)integerReturns the configured category's count in (asOfSecond - windowSeconds, asOfSecond].

Examples

For ExceptionThresholdMonitor([{"category":"A","threshold":5,"windowSeconds":1800}]):

StepOperationResult
1recordException("A", 0)false
2recordException("A", 1)false
3recordException("A", 2)false
4recordException("A", 3)false
5recordException("A", 4)false
6recordException("A", 5)true
7getRecentCount("A", 1800)5
8recordException("A", 1800)true

At time 1800, the occurrence at 0 is on the exclusive lower boundary and no longer counts. The final record restores the count to 6.

Constraints

  • policies contains between 1 and 10,000 objects with unique categories.
  • A category is a nonempty string of at most 100 Unicode scalar values.
  • 1 <= threshold <= 1,000,000
  • 1 <= windowSeconds <= 86,400
  • -9,000,000,000,000,000 <= occurredAtSecond, asOfSecond <= 9,000,000,000,000,000
  • At most 200,000 public method calls occur per testcase.
  • Null values, duplicate categories, and invalid policy fields are not supplied or judged.

Notes

For each category, time values across successful calls are nondecreasing. Different categories may use unrelated times. Testcase calls are sequential; concurrent access, wall-clock access, and out-of-order ingestion are outside scope.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.