Last Updated: 13 Feb, 2021

N Queue Using Array

Easy
Asked in companies
AdobeShopeeAmazon

Problem statement

You will be given ‘N’ queries. You need to implement ‘N’ queues using an array according to those queries. Each query will belong to one of these two types:

1 ‘X’ N: Enqueue element ‘X’  into the end of the nth queue. Returns true if the element is enqueued, otherwise false.

2 N: Dequeue the element at the front of the nth queue. Returns -1 if the queue is empty, otherwise, returns the dequeued element.
Note:
Please note that Enqueue means adding an element to the end of the queue, while Dequeue means removing the element from the front of the queue.
Input Format:
The first line of input contains three space-separated integers ‘N’, ‘S’ and ‘Q’ denoting the number of queues, the size of the array and number of queries, respectively. 

The next ‘Q’ lines specify the type of operation/query to be performed on the data structure.

Each query contains an integer ‘P’ denoting the type of query.

For query of type 1, the integer ‘P’ is equal to 1 and it is followed by two space-separated integers ‘X’ and ‘M’ denoting the element and queue on which operation is to be performed, respectively.

For query of type 2, the integer ‘P’ is equal to 2 and it is followed by a single integer ‘M’ denoting the queue on which operation is to be performed.
Output Format:
For each query, return the output returned after performing the corresponding operation on the data structure. 

Print the output of each test case in a separate line.
Note:
You don’t need to print anything, It has already been taken care of. You Just have to complete the given functions.
Constraints:
1 <= N <= S <= 1000
1 <= Q <= 10^5 
1 <= P <= 2
1 <= X <= 10^5

Time limit: 1 sec

Approaches

01 Approach

One approach that you might be thinking of is to divide the complete array into ‘N’ parts. Like if there are 3 queues that you need to implement in a single array of size 12, then you divide the array into ‘S’ / ‘N’ = 12 / 3 = 4 parts of equal size and then implement a single queue in different arrays. But, this approach will not be valid for all cases, that is it will give ‘overflow’ and ‘underflow’ conditions even if it is possible to solve those conditions. 

 

Example:

 

‘N’ = 3

‘S’ = 12

‘QUERIES’:  1 1 4 1 1 5 1 1 6 1 1 7 1 1 8

Now, in this case, we divide the array into 3 parts of size = 4 each. Now if we enqueue elements 4 5 6 7 into the first queue and then try to enqueue 8 into the same, the array will show filled and the problem will return the ‘Overflow’ condition even when the array still has empty spaces where the element could be enqueued. 

This makes this approach limited.

 

So, now we will work on a better approach that would solve this problem.

 

In this approach, we will use three arrays to store different indexes, which will help us to implement N queues into a single main array. Let us call these three arrays as start[], end[], next[].

 

The start array will store the indexes of the front elements of all the queues. The end array will store the indexes of the rear elements of all the queues. And finally, the next array will store the indexes of the next value for all values of the main array.

 

Apart from these, we will also initialize a stack that will store the indexes of the empty slots in the main array. Empty slots are the slots that are not yet filled. We will also store the top of this stack as a variable let’s say ‘TEMP’.

 

Now, after initializing all these data structures required, we move on to the algorithm part.

 

Algorithm: 

 

We need to complete two functions enqueue() and dequeue():

 

  1. enqueue(): (Function to enqueue an element into the given queue)
    • Check if the array is full.
      • If yes -> Overflow condition -> return -1
      • Else -> move forward.
    • Store index of the first empty slot from the temp variable to another variable let's say ‘INDEX’.
    • Update the index of the empty slot to the index of the next slot in the empty list.
    • Check if the queue is empty.
      • If yes, update the start element.
      • If no, update the next element of the end.
    • Update the next element and the end element for the given queue.
    • Finally, enqueue the value into the main array.
  2. dequeue(): (Function to dequeue the front element from the given queue)
    • Check if the array is already empty.
      • If yes -> Underflow condition -> return -1
      • Else -> move forward.
    • Find the index of the front element of the given queue and store it in a variable let’s say front.
    • Change the top of the stack to store the next element of the previous top element.
    • Now, attach the previous front element to the beginning of the empty list.
    • Finally, return the previous front element.