easyQueuePattern: Design
Implement Queue Data Structure Solution
Problem Statement
Design a queue data structure that supports standard queue operations: enqueuing and dequeuing, following the First-In-First-Out (FIFO) principle. Given an empty queue, your solution should provide methods to add elements and remove elements from the front of the queue.
Examples
Example 1:
Input:{enqueuing: [1, 2, 3], dequeuing: [1, 2]}
Output:{}
Explanation: Adding elements 1, 2, and 3 and then removing element 1 from the front yields the queue [2, 3].
Example 2:
Input:{enqueuing: [9, 10, 11], dequeuing: [9, 11]}
Output:{}
Explanation: Adding elements 9, 10, and 11 and then removing element 9 from the front yields the queue [10, 11].
Constraints
- Implement the queue operations in an efficient manner.
- Support both addition and removal of elements from the queue.
- Maintain a First-In-First-Out (FIFO) ordering in the queue.
Time: O(1) Space: O(n)
Implementing a double-ended queue using two arrays: one for storing elements and the other for keeping track of the front and rear of the queue.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
class QueueDataStructure: def __init__(self): self.queue = [] def enqueue(self, elements): if not isinstance(elements, list): elements = [elements]; self.queue.extend(elements); def dequeue(self): if not self.queue: return None; return self.queue.pop(0);