Circular Queue Implementation
Function Name: CircularQueue
Description
Implement a Circular Queue data structure using an array. The circular queue should support the following operations: enqueue, dequeue, peek, and isEmpty.
The enqueue operation should add an item to the back of the queue if there is space. If the queue is full, it should return 'Queue Full'.
The dequeue operation should remove an item from the front of the queue and return it. If the queue is empty, it should return 'Queue Empty'.
The peek operation should return the item at the front of the queue without removing it. If the queue is empty, it should return 'Queue Empty'.
The isEmpty operation should check if the queue is empty and return a boolean value.
Requirements
- The CircularQueue constructor function should take one parameter, the maximum size of the queue.
- Do not use built-in language features that mimic the behavior of a queue directly.
- You must handle the wrap-around behavior characteristic of a circular queue.
- Ensure that your implementation avoids the 'Queue Overflow' and 'Queue Underflow' problems.
- Write helper methods as needed to keep your code modular and readable.
Examples
let cq = new CircularQueue(3); // creates a circular queue with a max size of 3cq.enqueue(10); // queue is now [10]cq.enqueue(20); // queue is now [10, 20]cq.enqueue(30); // queue is now [10, 20, 30]cq.enqueue(40); // returns 'Queue Full'cq.dequeue(); // returns 10, queue is now [20, 30]cq.peek(); // returns 20cq.isEmpty(); // returns falsecq.dequeue(); // returns 20cq.dequeue(); // returns 30cq.isEmpty(); // returns true
Links
Unique Paths Calculator
Fri Sep 20 2024
Create a function that calculates the number of unique paths from the top-left corner to the bottom-right corner of a m x n grid. The only movements allowed are to move right or down at any given point. The function will take two parameters: the number of rows (m) and the number of columns (n). It should return the number of unique paths possible.Constraints:1. You may not use any third-party libraries.2. Optimize the function for time complexity.3. Extra challenge: Use only O(n) extra space for the solution, where n is the number of columns.
Simple Time Formatter
Sun Sep 22 2024
Write a function that converts 12-hour time format to 24-hour format. The function will take a string representing the time as its parameter. It must return the converted time as a string in the format 'HH:MM', ensuring 'HH' and 'MM' are both two digits.