Consider a stack and a queue . Both of them are initially empty and have the capacity to store ten elements each. The elements , and arrive one by one, in that order. When an element arrives, it is assigned either to (pushed on ) or to (enqueued to ). Once all the five elements are stored, the output is generated in two steps. First, stack S is emptied by popping all elements. Then queue is emptied by dequeueing all elements. The output obtained by following this process is .
Given the output, the objective is to predict whether an element was assigned to or .
Which of the following options is/are possible valid assignment(s) of the elements?
Note: In the options, the notation denotes that element was assigned to and denotes that element was assigned to .
View worked solution
Answer: A;B
To find the valid assignments, let us understand how the output is generated:
- Stack (LIFO): If elements are pushed into in the order of their arrival, popping them all will output them in the reverse order of their arrival.
- Queue (FIFO): If elements are enqueued into in the order of their arrival, dequeueing them all will output them in the same order as their arrival.
The final output is the concatenation of the popped elements from followed by the dequeued elements from :
We need the final output to be . Let us test each option:
Option A:
- Elements assigned to : (in arrival order)
- Elements assigned to : (in arrival order)
- Output from (reversed):
- Output from (same order):
- Combined Output: (Matches)
Option B:
- Elements assigned to : (in arrival order)
- Elements assigned to : (in arrival order)
- Output from (reversed):
- Output from (same order):
- Combined Output: (Matches)
Option C:
- Elements assigned to : (in arrival order)
- Elements assigned to : (in arrival order)
- Output from (reversed):
- Output from (same order):
- Combined Output: (Does not match)
Option D:
- Elements assigned to : (in arrival order)
- Elements assigned to : (in arrival order)
- Output from (reversed):
- Output from (same order):
- Combined Output: (Does not match)
Thus, the valid assignments are A and B.
Free practice · Your answers on this page are not saved.
