Circular Queue Implementation
Function Name: CircularQueue
Description
Create a CircularQueue class that represents a circular queue for integers with a fixed size.
It should support enqueue (add an item), dequeue (remove and return an item), and isEmpty methods.
Additionally, the CircularQueue class should have a method to return the current size of the queue.
Requirements
- The CircularQueue class should have a constructor that takes an integer indicating the maximum size of the queue.
- The enqueue method should add an element to the back of the queue if the queue has not reached its maximum size. If the queue is full, it should return 'Queue is full'.
- The dequeue method should remove the element at the front of the queue and return it. If the queue is empty, it should return 'Queue is empty'.
- The isEmpty method should return true if the queue is empty, false otherwise.
- The size method should return the number of elements currently in the queue.
- You should not use any built-in queue data structures.
- Think about how you will handle the circular aspect; when an item is dequeued, the space it occupied can be used to enqueue new items.
Examples
let cq = new CircularQueue(3);cq.enqueue(1); // returns nothingcq.enqueue(2); // returns nothingcq.size(); // returns 2cq.enqueue(3); // returns nothingcq.enqueue(4); // returns 'Queue is full'cq.dequeue(); // returns 1cq.enqueue(4); // returns nothingcq.size(); // returns 3
Prime Factorization
Fri Aug 23 2024
Write a function that computes the prime factors of a given number, n.A prime factor is a factor that is a prime number, and prime factorization is the determination of the set of prime numbers which multiply together to give the original integer.The function should return an array of integers that represents the prime factors of the given number in ascending order.
Battleship Hit Detector
Sun Aug 25 2024
In the game of Battleship, players try to guess the locations of each other's ships on a grid. Given a grid with the location of a single ship marked with 'S' and the rest of the cells filled with 'O', write a function that determines if a given shot hits the ship. The grid will be represented as a 2D array, and the function will take the grid and coordinates of the shot. The function should return 'Hit' if the coordinates have an 'S', otherwise, return 'Miss'.