Generic Queue Implementation in TypeScript
This challenge asks you to implement a generic Queue data structure in TypeScript. Queues are fundamental data structures that follow the First-In, First-Out (FIFO) principle, making them useful for managing tasks, handling requests, and various other scenarios where order matters. Successfully implementing a generic queue allows you to store and manage any type of data in a queue-like manner.
Problem Description
You are required to create a Queue class in TypeScript that can hold elements of any type, parameterized by a generic type T. The Queue class should provide the following methods:
enqueue(item: T): void: Adds an element to the rear of the queue.dequeue(): T | undefined: Removes and returns the element at the front of the queue. Returnsundefinedif the queue is empty.peek(): T | undefined: Returns the element at the front of the queue without removing it. Returnsundefinedif the queue is empty.isEmpty(): boolean: Returnstrueif the queue is empty,falseotherwise.size(): number: Returns the number of elements in the queue.
The implementation should be efficient and handle edge cases gracefully. Consider how to manage the queue's internal storage (e.g., using an array) and ensure proper FIFO behavior.
Examples
Example 1:
Input:
queue.enqueue("apple");
queue.enqueue("banana");
queue.enqueue("cherry");
Output: "apple"
Explanation: `dequeue()` removes and returns the first element, "apple".
Input:
queue.dequeue();
Output: "banana"
Explanation: `dequeue()` removes and returns the next element, "banana".
Example 2:
Input:
const numberQueue = new Queue<number>();
numberQueue.enqueue(1);
numberQueue.enqueue(2);
numberQueue.peek();
Output: 1
Explanation: `peek()` returns the front element (1) without removing it.
Input:
numberQueue.isEmpty();
Output: false
Explanation: The queue is not empty.
Example 3: (Edge Case - Empty Queue)
Input:
const emptyQueue = new Queue<string>();
emptyQueue.dequeue();
Output: undefined
Explanation: `dequeue()` returns `undefined` because the queue is empty.
Input:
emptyQueue.peek();
Output: undefined
Explanation: `peek()` returns `undefined` because the queue is empty.
Input:
emptyQueue.isEmpty();
Output: true
Explanation: The queue is empty.
Constraints
- The queue should be able to handle a large number of enqueue and dequeue operations without significant performance degradation. While a linked list implementation is acceptable, an array-based implementation is preferred for simplicity and potential performance benefits in many common use cases.
- The
enqueueanddequeueoperations should have an average time complexity of O(1). - The generic type
Tcan be any valid TypeScript type. - The
size()method should return the correct number of elements in the queue at any given time.
Notes
- Consider using a JavaScript array internally to store the queue elements.
- Think about how to efficiently manage the front and rear of the queue when using an array.
- Pay close attention to edge cases, such as attempting to dequeue from an empty queue.
- Ensure your code is well-documented and easy to understand. Clear variable names and comments are encouraged.
- While you can use external libraries, the goal is to demonstrate your understanding of queue implementation principles. Avoid relying on complex external queue implementations.