Tutorialspoint
Problem
Solution
Submissions

Queue using Arrays

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 8

Write a JavaScript program to implement a Queue data structure using arrays. The queue should support basic operations like enqueue (add element), dequeue (remove element), front (get front element), and isEmpty (check if queue is empty).

Example 1
  • Input: Operations: ["enqueue", "enqueue", "front", "dequeue", "isEmpty"], Values: [1, 2, null, null, null]
  • Output: [null, null, 1, 1, false]
  • Explanation:
    • enqueue(1) - Add 1 to the queue. Queue: [1].
    • enqueue(2) - Add 2 to the queue. Queue: [1, 2].
    • front() - Return front element which is 1.
    • Queue: [1, 2]. dequeue() - Remove and return front element 1.
    • Queue: [2]. isEmpty() - Check if queue is empty.
    • Returns false since queue has element 2.
Example 2
  • Input: Operations: ["enqueue", "dequeue", "isEmpty", "front"], Values: [5, null, null, null]
  • Output: [null, 5, true, null]
  • Explanation:
    • enqueue(5) - Add 5 to the queue.
    • Queue: [5]. dequeue() - Remove and return front element 5.
    • Queue: []. isEmpty() - Check if queue is empty.
    • Returns true since queue is empty.
    • front() - Try to get front element of empty queue.
    • Returns null or undefined.
Constraints
  • 1 ≤ operations.length ≤ 1000
  • -1000 ≤ value ≤ 1000 for enqueue operations
  • At most 300 calls will be made to enqueue and dequeue
  • All operations are valid (no dequeue or front on empty queue)
  • Time Complexity: O(1) for all operations
  • Space Complexity: O(n) where n is number of elements
ArraysQueuePwCSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use an array to store queue elements and maintain front and rear pointers
  • Enqueue operation adds element at the rear of the queue
  • Dequeue operation removes element from the front of the queue
  • Keep track of the size of the queue for isEmpty operation
  • Handle edge cases like empty queue for front and dequeue operations
  • Consider using circular array approach for better space utilization

Steps to solve by this approach:

 Step 1: Initialize a Queue class with an array and front/rear index pointers.

 Step 2: Implement enqueue method to add elements at the rear index position.
 Step 3: Increment rear index after adding each element to maintain queue order.
 Step 4: Implement dequeue method to remove element from front index position.
 Step 5: Increment front index after removing element and return the removed value.
 Step 6: Implement front method to return element at front index without removal.
 Step 7: Implement isEmpty method by comparing front and rear indices.
 Step 8: Handle edge cases for empty queue in dequeue and front operations.

Submitted Code :