Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Input: MyQueue q = new MyQueue(); q.push(1); q.push(2); q.peek(); // 1; q.pop(); // 1; q.empty(); // false
Output: [null, null, null, 1, 1, false]
Topics: queue, stack
Asked by: Amazon, Microsoft, Google, Meta, Bloomberg
Time complexity: O(1) Amortized. Space complexity: O(n).