FastPrepPeriodic Event Loop

Periodic Event Loop

Etched logoEtched● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

At time zero, register periodic events. Event i first executes at periods[i] and should execute at each multiple of that period until it has run runLimits[i] times. A run limit models a callback returning false after that execution.

Execute only events due at or before horizon. When several events are due together, run smaller eventIds first. Return an execution log of strings time:eventId. Event IDs are unique.

Function

runPeriodicEvents(eventIds: String[], periods: int[], runLimits: int[], horizon: int) → String[]

Examples

Example 1

eventIds = ["beat","flush"]periods = [2,3]runLimits = [3,2]horizon = 6return = ["2:beat","3:flush","4:beat","6:beat","6:flush"]

The events execute at 2:beat, 3:flush, 4:beat, then both at time 6 in ID order.

Example 2

eventIds = ["z","a"]periods = [4,4]runLimits = [1,1]horizon = 4return = ["4:a","4:z"]

Equal due times are ordered lexicographically by event ID.

Constraints

  • 0 <= eventIds.length = periods.length = runLimits.length <= 100000
  • 1 <= periods[i], horizon <= 1000000000
  • 0 <= runLimits[i] <= 100000
  • The returned log contains at most 200000 entries.

More Etched problems

See Etched hiring insights
public String[] runPeriodicEvents(String[] eventIds, int[] periods, int[] runLimits, int horizon) {
  // Write your code here.
}
eventIds["beat","flush"]
periods[2,3]
runLimits[3,2]
horizon6
expected["2:beat", "3:flush", "4:beat", "6:beat", "6:flush"]
Checking account…