Question 1
Following is pseudo code of a function that takes a Queue as an argument, and uses a stack S to do processing.
void fun(Queue *Q) {
Stack S; // Say it creates an empty stack S
// Run while Q is not empty
while (!isEmpty(Q)) {
// deQueue an item from Q and push the dequeued item to S
push(&S, deQueue(Q));
}
// Run while Stack S is not empty
while (!isEmpty(&S)) {
// Pop an item from S and enqueue the popped item to Q
enQueue(Q, pop(&S));
}
}
void fun(Queue *Q)
{
Stack S; // Say it creates an empty stack S
// Run while Q is not empty
while (!isEmpty(Q))
{
// deQueue an item from Q and push the dequeued item to S
push(&S, deQueue(Q));
}
// Run while Stack S is not empty
while (!isEmpty(&S))
{
// Pop an item from S and enqueue the popped item to Q
enQueue(Q, pop(&S));
}
}
void fun(Queue Q) {
Stack S = new Stack(); // Say it creates an empty stack S
// Run while Q is not empty
while (!Q.isEmpty()) {
// deQueue an item from Q and push the dequeued item to S
S.push(Q.deQueue());
}
// Run while Stack S is not empty
while (!S.isEmpty()) {
// Pop an item from S and enqueue the popped item to Q
Q.enQueue(S.pop());
}
}
def fun(Q):
S = [] # Say it creates an empty stack S
# Run while Q is not empty
while not Q.is_empty():
# deQueue an item from Q and push the dequeued item to S
S.append(Q.deQueue())
# Run while Stack S is not empty
while S:
# Pop an item from S and enqueue the popped item to Q
Q.enQueue(S.pop())
function fun(Q) {
let S = []; // Say it creates an empty stack S
// Run while Q is not empty
while (!Q.isEmpty()) {
// deQueue an item from Q and push the dequeued item to S
S.push(Q.deQueue());
}
// Run while Stack S is not empty
while (S.length > 0) {
// Pop an item from S and enqueue the popped item to Q
Q.enQueue(S.pop());
}
}
What does the above function do in general?
Removes the last from Q
Keeps the Q same as it was before the call
Makes Q empty
Reverses the Q
Question 2
How many stacks are needed to implement a queue. Consider the situation where no other data structure like arrays, linked list is available to you.
1
2
3
4
Question 3
Which of the following operations on a queue data structure has a time complexity of O(1)?
A) Enqueue
B) Dequeue
C) Peek
D) Clear
A and B
B only
C only
A and D
Question 4
A priority queue can efficiently implemented using which of the following data structures? Assume that the number of insert and peek (operation to see the current highest priority item) and extraction (remove the highest priority item) operations are almost same.
Array
Linked List
Heap Data Structures like Binary Heap, Fibonacci Heap
None of the above
Question 5
Which of the following is true about linked list implementation of queue?
In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end.
In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.
Both of the above
None of the above
Question 6
A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is given below: 10, 8, 5, 3, 2 Two new elements ”1‘ and ”7‘ are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
10, 8, 7, 5, 3, 2, 1
10, 8, 7, 2, 3, 1, 5
10, 8, 7, 1, 2, 3, 5
10, 8, 7, 3, 2, 1, 5
Question 7
An implementation of a queue Q, using two stacks S1 and S2, is given below:
Function Insert(Q, x):
Push x onto stack S1
Function Delete(Q):
If stack S2 is empty:
If stack S1 is also empty:
Print "Q is empty"
Return
Else:
While stack S1 is not empty:
Pop element from stack S1 and store it in x
Push x onto stack S2
Pop element from stack S2 and store it in x
Return x // Or process the popped element (if needed)
Let n insert and m (<=n) delete operations be performed in an arbitrary order on an empty queue Q. Let x and y be the number of push and pop operations performed respectively in the process. Which one of the following is true for all m and n?
n+m <= x < 2n and 2m <= y <= n+m
n+m <= x < 2n and 2m<= y <= 2n
2m <= x < 2n and 2m <= y <= n+m
2m <= x <2n and 2m <= y <= 2n
Question 8
Consider the following operation along with Enqueue and Dequeue operations on queues, where k is a global parameter.
MultiDequeue(Q){
m = k
while (Q is not empty and m > 0) {
Dequeue(Q)
m = m - 1
}
}
What is the worst case time complexity of a sequence of n MultiDequeue() operations on an initially empty queue? (A) [Tex]\\Theta(n) [/Tex](B) [Tex]\\Theta(n + k) [/Tex](C) [Tex]\\Theta(nk) [/Tex](D) [Tex]\\Theta(n^2) [/Tex]
A
B
C
D
Question 9
Consider the following pseudo-code. Assume that IntQueue is an integer queue. What does the function fun do?
fun(int n)
{ IntQueue q = new IntQueue();
q.enqueue(0); q.enqueue(1);
for (int i = 0; i < n; i++)
{
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
print(a);
}
}
Prints numbers from 0 to n-1
Prints numbers from n-1 to 0
Prints first n Fibonacci numbers
Prints first n Fibonacci numbers in reverse order.
Question 10
Which of the following is NOT a common operation in a queue data structure?
Enqueue
Dequeue
Peek
Shuffle
There are 30 questions to complete.