Circular Queue Implementation
Function Name: CircularQueue
Description
Implement a Circular Queue data structure with a fixed size using only primitive data structures (arrays, not language-specific collections). The CircularQueue class should support the following operations: enqueue, dequeue, peek, and isEmpty.
The enqueue operation adds an item to the back of the queue, the dequeue operation removes an item from the front of the queue, peek returns the front element without removing it, and isEmpty indicates whether the queue is empty.
If the queue is full and an enqueue operation is attempted, the function should return false or indicate that the queue is full. When dequeue is called on an empty queue, it should return null or an appropriate indicator of emptiness.
Requirements
- The CircularQueue class should be initialized with a fixed size.
- Enqueue should insert an element at the end of the queue if it is not full.
- Dequeue should remove an element from the beginning of the queue if it is not empty.
- Peek should return the element at the beginning of the queue without removing it.
- IsEmpty should return true if the queue is empty, otherwise false.
- The implementation should not use language-specific collections or libraries, only arrays or basic data containers.
Examples
let cq = new CircularQueue(3);cq.isEmpty(); // returns truecq.enqueue(1); // returns truecq.enqueue(2); // returns truecq.peek(); // returns 1cq.enqueue(3); // returns truecq.enqueue(4); // returns false, because the queue is fullcq.dequeue(); // returns 1cq.dequeue(); // returns 2cq.isEmpty(); // returns falsecq.dequeue(); // returns 3cq.isEmpty(); // returns true
Links
Balanced Subarrays
Wed Aug 07 2024
The goal of this challenge is to write a function named 'findBalancedSubarrays' that takes in an array of integers and returns the total number of contiguous subarrays where the sum of elements on the left is equal to the sum of elements on the right.A subarray consists of one or more consecutive elements of the array. For subarrays with an odd number of elements, the middle element should be considered as belonging to neither side.
Colorful Number
Fri Aug 09 2024
A colorful number is a number where the product of every digit of a contiguous subset of its digits is different. For example, 263 is a colorful number because (2), (6), (3), (2*6), (6*3), (2*6*3) are all different. However, 236 is not colorful since (2*3) and (6) are both equal to 6.Write a function `isColorful` that takes a positive integer and returns a boolean indicating whether the number is colorful or not.