Circular Queue Implementation
Function Name: CircularQueue
Description
Implement a Circular Queue using an array. The Circular Queue 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, and the peek operation retrieves the item at the front of the queue without removing it. The isEmpty operation should return a boolean indicating whether the queue is empty. The Circular Queue should also have a fixed size specified at the time of creation and should handle the wrap-around when the end of the array is reached.
Requirements
- The CircularQueue class must have a constructor that takes a fixed size as an argument.
- Implement the enqueue operation, which should add an item to the queue if there is space available.
- Implement the dequeue operation, which should remove an item from the front of the queue and return it.
- Implement the peek operation which returns the item at the front of the queue without removing it.
- The isEmpty operation should return a boolean indicating whether the queue is empty.
- Enqueue should not add an item to the queue if the queue is full; it should return an error message or status instead.
- Dequeue on an empty queue should return an error message or status.
- Handle the corner case where enqueue should add to the beginning of the array when the end is reached and there is space available at the beginning (the wrap-around functionality).
Examples
const queue = new CircularQueue(3);queue.enqueue(1); // truequeue.enqueue(2); // truequeue.enqueue(3); // truequeue.enqueue(4); // Error: Queue is fullqueue.peek(); // returns 1queue.dequeue(); // returns 1queue.isEmpty(); // returns falsequeue.dequeue(); // returns 2queue.dequeue(); // returns 3queue.isEmpty(); // returns truequeue.dequeue(); // Error: Queue is emptyqueue.enqueue(5); // true, starts from the beginning
Min Stack Implementation
Wed Nov 27 2024
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.Implement the 'MinStack' class:- 'MinStack()' initializes the stack object.- 'void push(int val)' pushes the element val onto the stack.- 'void pop()' removes the element on the top of the stack.- 'int top()' gets the top element of the stack.- 'int getMin()' retrieves the minimum element in the stack.* You must implement a solution with O(1) time complexity for each function.
Array Pair Sum Divisibility
Fri Nov 29 2024
In this challenge, you are given an array of integers and a positive integer 'k'. Your task is to write a function that checks whether it is possible to divide the array into pairs such that the sum of each pair is divisible by 'k'.The function should return a boolean value: true if such a division is possible, or false if it is not. Each element of the array can only be used in one pair.