Implement a FIFO Queue
Updated
Problem statement
Implement the behavior of a first-in, first-out queue without using a built-in queue or deque type.
Process the paired arrays operations and values from left to right:
push: appendvalues[i]to the back of the queue.pop: remove the value at the front and append that value to the result.peek: append the value at the front to the result without removing it.
Return all values produced by pop and peek, in operation order. A push operation produces no result.
Function
queueOperations(operations: String[], values: int[]) → int[]Examples
Example 1
operations = ["push","push","peek","pop","peek"]values = [4,7,0,0,0]return = [4,4,7]The first peek reads 4. The following pop removes and returns 4, so the final peek reads 7.
Example 2
operations = ["push","push","pop","push","peek","pop","pop"]values = [10,20,0,30,0,0,0]return = [10,20,20,30]After removing 10, pushing 30 leaves the queue as [20,30]. The remaining operations read and remove those values in FIFO order.
Example 3
operations = ["push","push","peek","pop","peek","pop"]values = [-2,-2,0,0,0,0]return = [-2,-2,-2,-2]Equal and negative values retain their insertion order. A peek does not remove the front value.
Constraints
1 <= operations.length == values.length <= 100000.- Each operation is
push,pop, orpeek. - For
push,-10^9 <= values[i] <= 10^9; for other operations,values[i]is ignored. - Every
poporpeekoccurs while the queue is nonempty. - Do not use a built-in queue or deque type.
More ByteDance Phone Screen questions
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- 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
- Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience