Problem · Array

Filter and Sort Scheduled Tasks

Learn this problem
EasyRippling logoRipplingFULLTIMEONSITE INTERVIEW

Problem statement

Given parallel arrays describing scheduled tasks and a current timestamp, return the IDs of all eligible tasks in execution order.

A task at index i is eligible exactly when:

  • statuses[i] is PENDING; and
  • scheduledTimes[i] is at most currentTime.

Sort eligible tasks by:

  1. priorities[i] descending;
  2. scheduledTimes[i] ascending; then
  3. taskIds[i] lexicographically ascending.

Return the ordered task IDs. The judged practice combines the reported filtering and sorting capabilities into one result; it does not invent a separate undisclosed third part.

Function

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

Examples

Example 1

taskIds = ["task-a","task-b","task-c","task-d"]scheduledTimes = [10,5,10,1]priorities = [2,3,3,9]statuses = ["PENDING","PENDING","RUNNING","PENDING"]currentTime = 10return = ["task-d","task-b","task-a"]

task-c is not pending. The other tasks are due, so priority places task-d first, followed by task-b and task-a.

Example 2

taskIds = ["z","a","b","late"]scheduledTimes = [7,7,3,11]priorities = [5,5,5,9]statuses = ["PENDING","PENDING","PENDING","PENDING"]currentTime = 7return = ["b","a","z"]

late is not due. The other tasks have equal priority, so the earlier timestamp places b first and the lexicographic tie-break places a before z.

Example 3

taskIds = ["done","future"]scheduledTimes = [1,100]priorities = [10,10]statuses = ["COMPLETED","PENDING"]currentTime = 50return = []

The completed task is ineligible and the pending task is not due, so the result is empty.

Constraints

  • 0 <= taskIds.length <= 10^5.
  • All four task arrays have the same length.
  • Task IDs are unique non-empty strings.
  • 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[] filterAndSortTasks(String[] taskIds, long[] scheduledTimes, int[] priorities, String[] statuses, long currentTime) {
  // write your code here
}
taskIds["task-a","task-b","task-c","task-d"]
scheduledTimes[10,5,10,1]
priorities[2,3,3,9]
statuses["PENDING","PENDING","RUNNING","PENDING"]
currentTime10
expected["task-d", "task-b", "task-a"]
checking account