Next Greater Element on a Stream
Updated
Problem statement
Numbers arrive one at a time. stream[i] is the number that arrives at step i, counting from 0. The next greater element of the number at step j is the first number that arrives after step j and is strictly greater than stream[j].
Process the numbers in arrival order. When the number at step i arrives, report every earlier step j whose next greater element is stream[i]. These are exactly the steps whose answer becomes known at step i.
Return an array answer of length stream.length, where answer[i] lists those steps j in increasing order. A step whose number is never followed by a greater number is never reported.
Try to process each number as it arrives, keeping in memory only the steps whose answer is still unknown.
Function
streamNextGreater(stream: int[]) → int[][]Examples
Example 1
stream = [2,1,3,2,5]return = [[],[],[0,1],[],[2,3]]Steps 0 and 1 bring 2 and 1. Nothing earlier is smaller, so nothing is reported.
Step 2 brings 3, which is the first greater number after 2 (step 0) and after 1 (step 1), so it reports [0,1]. Step 3 brings 2, which is not greater than 3.
Step 4 brings 5, which answers step 2 (value 3) and step 3 (value 2), so it reports [2,3].
Example 2
stream = [5,4,4,6]return = [[],[],[],[0,1,2]]The second 4 is not greater than the first 4, so it answers nothing. The 6 at step 3 is the first greater number for all three earlier steps.
Example 3
stream = [3,3,1]return = [[],[],[]]No number is ever followed by a strictly greater one, so every step reports an empty list.
Example 4
stream = [1,2,3]return = [[],[0],[1]]Each number answers only the step right before it. When 3 arrives, step 0 was already answered by 2, so step 2 reports only [1].
Constraints
1 <= stream.length <= 10^5-10^9 <= stream[i] <= 10^9
More Amazon Onsite Interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Next Greater Element to the RightTekion · ONSITE INTERVIEWTekion interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Daily TemperatureseBay · ONSITE INTERVIEWeBay interview questions
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Flatten a Nested ArrayFreshworks · ONSITE INTERVIEWFreshworks interview questions
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)