FastPrepDynamic Longest-Waiting Unique Task Queries

Dynamic Longest-Waiting Unique Task Queries

Amazon logoAmazon● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

The arrays tasks and arrivalTimes describe task sightings. Sighting i records identifier tasks[i] at integer time arrivalTimes[i]. Arrival times are nondecreasing, and input order breaks ties between sightings at the same time.

For each value in queryTimes, consider every sighting whose arrival time is less than or equal to that query time. A task is waiting at that time only when its identifier has appeared exactly once in the considered prefix.

Return the waiting task whose only sighting occurs earliest. If eligible tasks share an arrival time, choose the one whose sighting appears first in the input. Return the empty string when no task is waiting.

Answer queries in their original order. Query times may be given in any order, and future sightings do not affect an earlier query.

Sibling variants: same core, different skin

These four problems are sibling variants of the same first-unique pattern. Their story wrappers and query timing differ, while the core rule keeps the earliest value whose frequency is exactly one.

  1. Longest-Waiting Unique Task
  2. Dynamic Longest-Waiting Unique Task Queries (current)
  3. First User to Log In Exactly Once
  4. First Unique Character in a Stream

Function

longestWaitingAtTimes(tasks: String[], arrivalTimes: int[], queryTimes: int[]) → String[]

Examples

Example 1

tasks = ["A","B","C","D","A"]arrivalTimes = [1,2,3,4,5]queryTimes = [4,6,1,0]return = ["A","B","A",""]

At time 4, every seen task is unique and A arrived first. By time 6, the second A removes it, so B is first. At time 1, only A has arrived; at time 0, no task has arrived.

Example 2

tasks = ["job","job","x"]arrivalTimes = [2,2,2]queryTimes = [2,1]return = ["x",""]

At time 2, both job sightings and the x sighting are included. Only x appears once. No task has arrived by time 1.

Example 3

tasks = ["red","blue","red","blue"]arrivalTimes = [1,3,7,8]queryTimes = [1,4,7,8]return = ["red","red","blue",""]

red is first until its second sighting at time 7. Then blue is the only waiting task until it also repeats at time 8.

Constraints

  • 1 <= tasks.length = arrivalTimes.length <= 100000.
  • 1 <= queryTimes.length <= 100000.
  • 0 <= arrivalTimes[i], queryTimes[i] <= 10^9.
  • arrivalTimes is nondecreasing.
  • Each task identifier is a nonempty alphanumeric string of length at most 40.
  • Task identifiers are case-sensitive.

More Amazon problems

See Amazon hiring insights
public String[] longestWaitingAtTimes(String[] tasks, int[] arrivalTimes, int[] queryTimes) {
    // write your code here
}
tasks["A","B","C","D","A"]
arrivalTimes[1,2,3,4,5]
queryTimes[4,6,1,0]
expected["A", "B", "A", ""]
Checking account…