Implement Queue using Stack

Learning DSA practice problem on Onlearn.

Difficulty: medium.

Topics: How to implement a queue using stacks?, Data Structures, Stack, Queue, Time Complexity, Space Complexity, Big O Notation, fifo/lifo properties, space complexity, stack, queue, data structures, time complexity analysis.

Implement a Queue data structure using two Stacks. Your implementation should support the following operations: push(x): Pushes element x to the back of the queue. pop(): Removes the element from the front of the queue and returns it. You may assume that the queue will not be empty when pop is called. peek(): Returns the element at the front of the queue. You may assume that the queue will not be empty when peek is called. empty(): Returns true if the queue is empty, false otherwise. size(): Returns the number of elements in the queue. Input Specification: Operations will be performed on an initially empty queue. Each test case will consist of a sequence of calls to the push, pop, peek, empty, and size methods. Output Specification: For pop and peek operations, return the appropriate integer value. For empty operations, return a boolean value. For size operations, return an integer representing the current size. Constraints: 1 <= x <= 10^9 At most 100 calls will be made to push, pop, peek, empty, and size. All calls to pop and peek will be valid (i.e., the queue will not be empty). Sample Test Case: Input (Operations Sequence): Expected Output: Explanation of Sample Test Case: 1. push(3): Queue: [3] 2. push(4): Queue: [3, 4] 3. pop(): Returns 3. Queue: [4] 4. push(5): Queue: [4, 5] 5. peek(): Returns 4. 6. size(): Returns 2. Difficulty: Easy to Medium