A message bus only offers a first-in-first-out queue (add at the back, remove from the front, read the front, size), but a feature needs last-in-first-out behavior. Write a class QueueStack that uses one queue and only those operations:
push(x) adds x.pop() removes and returns the most recently added item.top() returns the most recently added item.empty() returns whether the stack has no items.pop and top are only called when the stack is not empty.
Each example lists the calls in order, then each call's arguments. The first call creates the object with new QueueStack(…); the output lists what each call returns (null for the constructor and for methods that return nothing).
Input: ["QueueStack","push","push","top","pop","empty"] [[],[1],[2],[],[],[]] Output: [null,null,null,2,2,false]
2 was added last, so it comes out first, leaving 1.
Input: ["QueueStack","empty"] [[],[]] Output: [null,true]
Nothing has been added.
1 <= x <= 9100 calls.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #279 Stack From a Single Queue. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.