Implement a Queue Using an Array
Updated
Problem statement
Implement an initially empty first-in, first-out queue using an array. Process operations in order.
[0, value]addsvalueto the back.[1, 0]removes the oldest remaining value and produces that value. An empty dequeue produces-1.
Return the dequeue results in operation order. Enqueues produce no output. Use array storage and a head index so each operation takes constant amortized time; do not use a built-in queue or linked list.
Function
processQueue(operations: int[][]) → int[]Examples
Example 1
operations = [[0,4],[0,7],[1,0],[0,9],[1,0],[1,0]]return = [4,7,9]The dequeues return 4, then 7, then 9.
Example 2
operations = [[1,0],[0,-1],[1,0],[1,0]]return = [-1,-1,-1]The first and last dequeues are empty. The middle dequeue removes the stored value -1.
Example 3
operations = []return = []No operation produces a result.
Constraints
- 0 <= operations.length <= 100000.
- Every operation has exactly two integers.
- Every integer is between -1000000 and 1000000, inclusive.
- Each row is [0, value] for enqueue or [1, 0] for dequeue.
More London Stock Exchange interview questions
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Feb 2020
- Palindrome PartitioningPHONE SCREEN · Seen Feb 2020
- Two SumPHONE SCREEN · Seen Feb 2020
- Two Sum in a Sorted ArrayPHONE SCREEN · Seen Feb 2020
- Group Anagram StringsONSITE INTERVIEW · Seen Apr 2020
- Implement Key Equality and HashingONSITE INTERVIEW · Seen Apr 2020
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- High-Card Pot GameAffirm · PHONE SCREENAffirm interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Simulate a Queued Multi-Rule Rate LimiterSnowflake · PHONE SCREENSnowflake interview questions
- Bus Station Seat AllotmentInMobi · PHONE SCREENInMobi interview questions
- FIFO FizzBuzz Market MatchingGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions