Problem · Array

Filter, Sort, and Deduplicate Scheduled Tasks

Learn this problem
MediumRippling logoRipplingFULLTIMEPHONE SCREEN
See Rippling hiring insights

Problem statement

Parallel arrays describe snapshots of scheduled tasks. Record i contains taskIds[i], scheduledTimes[i], priorities[i], and statuses[i].

Process the records in three phases:

  1. Deduplicate: For each task ID, retain only its last occurrence in the input. That occurrence is the current snapshot of the task.
  2. Filter: A retained task is eligible exactly when its status is PENDING and its scheduled time is at most currentTime.
  3. Sort: Order eligible tasks by priority descending, scheduled time ascending, and task ID lexicographically ascending.

Return the ordered task IDs. Each ID appears at most once.

Function

filterSortAndDeduplicateTasks(taskIds: String[], scheduledTimes: long[], priorities: int[], statuses: String[], currentTime: long) → String[]

Examples

Example 1

taskIds = ["a","b","a","c","d"]scheduledTimes = [5,4,3,2,10]priorities = [1,5,9,5,8]statuses = ["PENDING","PENDING","COMPLETED","PENDING","PENDING"]currentTime = 5return = ["c","b"]

The last snapshot for a is completed, so a is filtered out. Task d is not due. Tasks c and b tie on priority, so the earlier scheduled time places c first.

Example 2

taskIds = ["x","y","x","y","z"]scheduledTimes = [1,1,2,10,2]priorities = [1,9,5,10,5]statuses = ["COMPLETED","PENDING","PENDING","PENDING","PENDING"]currentTime = 2return = ["x","z"]

The last x snapshot becomes eligible. The last y snapshot replaces an earlier eligible one but is not due. Tasks x and z tie on priority and time, so ID order breaks the tie.

Example 3

taskIds = []scheduledTimes = []priorities = []statuses = []currentTime = 0return = []

An empty record stream has no eligible tasks.

Constraints

  • 0 <= taskIds.length <= 10^5.
  • All four task arrays have the same length.
  • Task IDs are non-empty strings and may repeat.
  • Each timestamp fits in a signed 64-bit integer and each priority fits in a signed 32-bit integer.
  • Every status is PENDING, RUNNING, or COMPLETED.

More Rippling problems

drafts saved locally
public String[] filterSortAndDeduplicateTasks(String[] taskIds, long[] scheduledTimes, int[] priorities, String[] statuses, long currentTime) {
  // write your code here
}
taskIds["a","b","a","c","d"]
scheduledTimes[5,4,3,2,10]
priorities[1,5,9,5,8]
statuses["PENDING","PENDING","COMPLETED","PENDING","PENDING"]
currentTime5
expected["c", "b"]
checking account