Queue on a Vector
Updated
Problem statement
Implement a first-in, first-out queue that keeps all of its values in one vector, an array that grows when it runs out of room, and process operations on it. Do not use a built-in queue, deque or linked list.
The queue starts empty. Each operation is a pair of integers:
[0, value]: pushvalueto the back of the queue. Nothing is recorded.[1, 0]: pop the front value and record it, or record-1if the queue is empty.[2, 0]: record the front value without removing it, or-1if the queue is empty.[3, 0]: record the number of values in the queue.[4, 0]: record1if the queue is empty, otherwise0.
Return the recorded values in operation order. Each operation should take amortized O(1) time.
Note: amortized O(1) means that any m operations take O(m) time in total, even if a single operation is sometimes slower.
Function
vectorQueue(operations: int[][]) → int[]Examples
Example 1
operations = [[0,5],[0,8],[2,0],[1,0],[0,3],[3,0],[1,0],[1,0],[1,0],[4,0]]return = [5,5,2,8,3,-1,1]After pushing 5 and 8, the front is 5, and the pop removes it. After pushing 3, the queue holds 8 and 3, so its size is 2. The next pops return 8 and 3. The last pop finds the queue empty and returns -1, and the empty check records 1.
Example 2
operations = [[4,0],[0,1],[0,2],[0,3],[1,0],[0,4],[0,5],[0,6],[3,0],[1,0],[2,0]]return = [1,1,5,2,3]The queue starts empty, so the first check records 1. The pop returns 1, the first value pushed. Five values remain (2 to 6), then the pop returns 2 and the front is 3.
Constraints
1 <= operations.length <= 2 * 10^5operations[i].length == 20 <= value <= 10^9- The first integer of each operation is between 0 and 4. For operations 1 to 4, the second integer is 0.
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance 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
- Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions
- Zigzag Iterator Over Two ListsGoogle · PHONE SCREENGoogle interview questions
- Bounded Circular QueueDatadog · PHONE SCREENDatadog interview questions
- Moving Average of a Data StreamSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences