Dynamic Longest-Waiting Unique Task Queries
Updated
Problem statement
The arrays tasks and arrivalTimes list task sightings. Sighting i records the task identifier tasks[i] at integer time arrivalTimes[i]. Arrival times never decrease. When two sightings share a time, the one earlier in the input comes first.
For each value in queryTimes, look at every sighting whose arrival time is less than or equal to that query time. A task is waiting at that time only if its identifier appears exactly once among those sightings.
Return the waiting task whose only sighting came earliest. If several waiting tasks share that arrival time, pick the one whose sighting comes first in the input. Return the empty string when no task is waiting.
Answer the queries in their original order. Query times may come in any order. Sightings after a query time do not affect that query.
Sibling variants: same idea, different story
These four problems are sibling variants built on the same first-unique pattern. Their stories and query timing differ. In each one, the rule is to keep the earliest value that appears exactly once.
- Longest-Waiting Unique Task
- Dynamic Longest-Waiting Unique Task Queries (current)
- First User to Log In Exactly Once
- 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, tasks A through D have each arrived once. A came first, so the answer is A. By time 6, A has arrived a second time, so it is no longer waiting. B is now the earliest waiting task, so the answer is B. At time 1, only A has arrived, so the answer is A. At time 0, no task has arrived, so the answer is the empty string. The result is ["A","B","A",""].
Example 2
tasks = ["job","job","x"]arrivalTimes = [2,2,2]queryTimes = [2,1]return = ["x",""]At time 2, all three sightings count: job twice and x once. Only x appears exactly once, so the answer is x. No task has arrived by time 1, so that answer is the empty string. The result is ["x",""].
Example 3
tasks = ["red","blue","red","blue"]arrivalTimes = [1,3,7,8]queryTimes = [1,4,7,8]return = ["red","red","blue",""]For the first two queries, red is the earliest task seen only once. At time 7, red arrives a second time, so blue becomes the only waiting task. At time 8, blue also repeats, so no task is waiting and the answer is the empty string. The result is ["red","red","blue",""].
Constraints
1 <= tasks.length = arrivalTimes.length <= 100000.1 <= queryTimes.length <= 100000.0 <= arrivalTimes[i], queryTimes[i] <= 10^9.arrivalTimesis nondecreasing.- Each task identifier is a nonempty alphanumeric string of length at most
40. - Task identifiers are case-sensitive.
More Amazon Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Build a Task and Subtask HierarchyStripe · PHONE SCREENStripe interview questions
- Find All Points Within a RadiusGoogle · PHONE SCREENGoogle interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)