Run a Queue Built From Stacks
Updated
Problem statement
A line of customers starts empty, and people are served in the order they arrived. Process commands from first to last; commands[i] is one of:
"enqueue": a new customer numberedvalues[i]joins the back of the line."dequeue": the customer at the head of the line is served and leaves; record their number. If nobody waits, record-1."front": record the number of the customer at the head without serving them, or-1when nobody waits."size": record how many customers are waiting.
Entries of values that sit beside a command other than "enqueue" are 0 and carry no meaning. The answer is every number you recorded, first to last. The line is meant to be built from two stacks and nothing else.
Function
runQueue(commands: String[], values: int[]) → int[]Examples
Example 1
commands = ["enqueue","enqueue","enqueue","dequeue","enqueue","front","size","dequeue","dequeue","dequeue"]values = [6,2,9,0,4,0,0,0,0,0]return = [6,2,3,2,9,4]The queue serves in arrival order. After 6, 2, 9 arrive, dequeue gives 6. Then 4 arrives at the back, front is 2 and size is 3. The last three dequeues give 2, 9 and 4.
Example 2
commands = ["enqueue","dequeue","dequeue","front","size"]values = [5,0,0,0,0]return = [5,-1,-1,0]The one customer is served, so the second dequeue finds nobody and records -1, and so does front; afterwards the line has 0 people.
Example 3
commands = ["enqueue","front","enqueue","front"]values = [8,0,3,0]return = [8,8]The front stays 8 after 3 joins the queue, because 3 stands behind it.
Constraints
commandsholds between1and3000entries.valueshas as many entries ascommands.- Each command is
"enqueue","dequeue","front"or"size". - Every value is between
0and10^5; it is0for every command except"enqueue".
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Implement Queue Using StacksMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Stack from Queues and Queue from StacksBank of America · ONSITE INTERVIEWBank of America interview questions
- Implement Queue Using StacksBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Implement a Queue Using Two StacksOracle · PHONE SCREENOracle interview questions
- Build a Queue From Two StacksAdobe · PHONE SCREENAdobe interview questions
- Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Command Undo Data StructureNetflix · ONSITE INTERVIEWNetflix interview questions
- A Queue Built From Two StacksExpedia · ONSITE INTERVIEWExpedia interview questions
- Implement a Basic StackAMD · ONSITE INTERVIEWAMD interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences