Problem Β· Sliding Window

Transaction Logs β€” Trigger / Resolve Alerts

Learn this problem
● MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Process error-log rows in nondecreasing timestamp order. Each row is "timestamp,merchant_id,status_code,count".

For each (merchant_id, status_code) pair, maintain the sum of count values in the sliding window (timestamp - windowSize, timestamp]. Before adding the current row, remove earlier rows for that pair whose timestamp is at most timestamp - windowSize; then add the current row's count.

Each pair starts below the threshold. Emit "TRIGGER merchant_id status_code" when its rolling sum crosses from below threshold to at least threshold. Emit "RESOLVE merchant_id status_code" when it crosses from at least the threshold to below it. Emit only on transitions and return events in processing order.

Function

processErrorLogs(logs: String[], windowSize: int, threshold: int) β†’ String[]

Examples

Example 1

logs = ["1,m1,E1,2","2,m1,E1,3","3,m2,E1,5","12,m1,E1,1"]windowSize = 10threshold = 5return = ["TRIGGER m1 E1","TRIGGER m2 E1","RESOLVE m1 E1"]
m1 reaches 5 at time 2. m2 independently reaches 5 at time 3. At time 12, m1's rows at times 1 and 2 have expired, so its sum becomes 1 and resolves.

Example 2

logs = ["5,shop,declined,7","6,shop,timeout,4","7,shop,declined,1","15,shop,declined,2"]windowSize = 10threshold = 8return = ["TRIGGER shop declined","RESOLVE shop declined"]
declined reaches 8 at time 7. At time 15, the count 7 from time 5 expires, leaving 1 + 2 = 3, so the pair resolves. timeout is tracked separately.

Constraints

  • logs are sorted by nondecreasing timestamp.
  • Every row is timestamp,merchant_id,status_code,count with a non-negative integer count.
  • windowSize > 0 and threshold > 0.
  • A row at time t0 is active at t exactly when t0 > t-windowSize.
  • Different merchant/status pairs are independent.

More Stripe problems

drafts saved locally
public String[] processErrorLogs(String[] logs, int windowSize, int threshold) {
  // write your code here
}
logs["1,m1,E1,2","2,m1,E1,3","3,m2,E1,5","12,m1,E1,1"]
windowSize10
threshold5
expected["TRIGGER m1 E1", "TRIGGER m2 E1", "RESOLVE m1 E1"]
checking account