FastPrepDelayed Job Scheduler

Delayed Job Scheduler

Walmart Labs logoWalmart Labs● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Implement a finite delayed-job scheduler. Process the parallel arrays actions, jobIds, times and delays in order. Start with no pending jobs. The operation clocks in times never decrease.

For this exercise, assume exactly two operations:

  • SCHEDULE adds the given unique positive job ID with due time times[i] + delays[i]. Return an empty row for this operation.
  • POLL returns and removes every pending job whose due time is at most times[i]. Return IDs ordered by due time, then by the earlier SCHEDULE operation when due times tie. An empty poll returns an empty row.

Return one integer-array row per operation, including empty rows. A zero-delay job may be released by a later poll at the same clock, but never by a poll that occurred before its schedule. Jobs not reached by a poll remain pending and produce no extra output.

The finite operations, clock and tie rules are explicit practice assumptions. This exercise schedules and releases jobs; it does not execute job bodies or add dependencies, cancellation, priorities, recurrence or profit optimization.

Function

runJobScheduler(actions: String[], jobIds: int[], times: int[], delays: int[]) → int[][]

Examples

Example 1

actions = ["SCHEDULE","SCHEDULE","POLL","POLL"]jobIds = [1,2,0,0]times = [0,1,2,5]delays = [5,1,0,0]return = [[],[],[2],[1]]

Jobs release by due time rather than insertion.

Example 2

actions = ["SCHEDULE","SCHEDULE","POLL"]jobIds = [8,3,0]times = [0,5,10]delays = [10,5,0]return = [[],[],[8,3]]

Equal due times keep insertion order not ID order.

Example 3

actions = []jobIds = []times = []delays = []return = []

Empty operation batch.

Constraints

  • The four arrays have equal length 0 <= n <= 128.
  • Every action is "SCHEDULE" or "POLL".
  • 0 <= times[i], delays[i] <= 10^6, and times never decrease.
  • Every scheduled ID is a distinct positive integer at most 10^9 and is never reused.
  • For POLL, the unused ID and delay fields are 0.

More Walmart Labs problems

See Walmart Labs hiring insights
public int[][] runJobScheduler(String[] actions, int[] jobIds, int[] times, int[] delays) {
    // Write your code here.
}
actions["SCHEDULE","SCHEDULE","POLL","POLL"]
jobIds[1,2,0,0]
times[0,1,2,5]
delays[5,1,0,0]
expected[[],[],[2],[1]]
Checking account…