FastPrepLongest-Waiting Unique Task

Longest-Waiting Unique Task

Amazon logoAmazon● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

The array tasks records task sightings in chronological order. A task is still waiting only when its identifier appears exactly once in the complete array.

Among all waiting tasks, return the identifier whose only sighting occurs earliest. This is the task that has waited the longest. If no task is waiting, return the empty string.

Task identifiers are compared case-sensitively.

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 (current)
  2. Dynamic Longest-Waiting Unique Task Queries
  3. First User to Log In Exactly Once
  4. First Unique Character in a Stream

Function

longestWaitingUniqueTask(tasks: String[]) → String

Examples

Example 1

tasks = ["A","B","C","D","A"]return = "B"

A appears twice, so it is not waiting. B, C, and D each appear once, and B occurs earliest.

Example 2

tasks = ["A","B","A","B"]return = ""

Both identifiers appear more than once, so no task remains waiting.

Example 3

tasks = ["build","deploy","test"]return = "build"

Every task appears once, so the first task has waited the longest.

Constraints

  • 1 <= tasks.length <= 100000.
  • 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 longestWaitingUniqueTask(String[] tasks) {
    // write your code here
}
tasks["A","B","C","D","A"]
expected"B"
Checking account…