Stack from Queues and Queue from Stacks
Updated
Problem statement
Build two separate structures and run a list of operations on them:
- a stack (last in, first out) that keeps its values only in queues, and
- a queue (first in, first out) that keeps its values only in stacks.
Inside the stack, use only normal queue operations: add to the back, remove from the front, look at the front, size and is-empty. Inside the queue, use only normal stack operations: push on top, pop from the top, look at the top, size and is-empty.
Each string in operations is one of the following:
"stack push x": push the integerxonto the stack."stack pop": remove the top value of the stack and output it."stack top": output the top value of the stack without removing it."stack empty": output"true"if the stack holds no values, otherwise"false"."queue push x": add the integerxto the back of the queue."queue pop": remove the front value of the queue and output it."queue peek": output the front value of the queue without removing it."queue empty": output"true"if the queue holds no values, otherwise"false".
Push operations output nothing. Return the outputs of all the other operations as strings, in the order the operations run. An operation on one structure never changes the other one.
Function
runOperations(operations: String[]) → String[]Examples
Example 1
operations = ["stack push 4","stack push 9","queue push 4","queue push 9","stack top","queue peek","stack pop","queue pop","stack empty","queue empty"]return = ["9","4","9","4","false","false"]Both structures get 4 and then 9. The stack gives back the value pushed last, so stack top and stack pop output 9. The queue gives back the value added first, so queue peek and queue pop output 4. Each structure still holds one value, so both empty checks output "false".
Example 2
operations = ["queue push 3","queue push 5","queue pop","queue push 7","queue pop","queue pop","queue empty"]return = ["3","5","7","true"]Values leave the queue in the order they arrived: 3, then 5, then 7, even though 7 was added after the first pop. After that the queue is empty.
Example 3
operations = ["stack push -2","stack pop","stack empty","stack push 6","stack push 1","stack top"]return = ["-2","true","1"]The stack pops -2 and is then empty. After 6 and 1 are pushed, 1 is on top.
Constraints
1 <= operations.length <= 2000- Each operation is one of the eight forms above, with single spaces between its words.
- In a push,
xis an integer with-10^4 <= x <= 10^4. stack popandstack toprun only when the stack is not empty, andqueue popandqueue peekrun only when the queue is not empty.
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Implement Queue Using StacksMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Implement Queue Using StacksBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Run a Queue Built From StacksCapital One · ONSITE INTERVIEWCapital One 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