Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Stack Queue

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Stack & Queue

Tutorial Sheet

Stacks
1) Which one of the following is an application of Stack Data Structure?
a) Managing function calls
b) The stock span problem
c) Arithmetic expression evaluation
d) All of the above

2) Which of the following is true about linked list implementation of stack?


a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
b) In push operation, if new nodes are inserted at the end, then in pop operation, nodes
must be removed from the beginning.
c) Both of the above
d) None of the above


3) Let S be a stack of size n >= 1. Starting with the empty stack, suppose we push the first n
natural numbers in sequence, and then perform n pop operations. Assume that Push and Pop
operation take X seconds each, and Y seconds elapse between the end of one such stack
operation and the start of the next operation. For m >= 1, define the stack-life of m as the time
elapsed from the end of Push(m) to the start of the pop operation that removes m from S. The
average stack-life of an element of this stack is
a) n(X+ Y)
b) 3Y + 2X
c) n(X + Y)-X
d) Y + 2X
4) A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from
opposite ends of the array. Variables top1 and top2 (top 1< top 2) point to the location of the
topmost element in each of the stacks. If the space is to be used efficiently, the condition for
“stack full” is
a) (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1)
b) top1 + top2 = MAXSIZE
c) (top1= MAXSIZE/2) or (top2 = MAXSIZE)
d) top1= top2 -1


5) Assume that the operators +, -, × are left associative and ^ is right associative. The order of
precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the
infix expression a + b × c - d ^ e ^ f is
a) abc × + def ^ ^ -
b) abc × + de ^ f ^ -
c) ab + c × d - e ^ f ^
d) - + a × bc ^ ^ def


6) A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push
(S, i)) = max (f(S), 0) + i for all stacks S and integers i. If a stack S contains the integers 2, -3,

0 1 2 3 4 5 6 7 8 9

2, -1, 2 in order from bottom to top, what is f(S)?


a) 6
b) 4
c) 3
d) 2


7) Suppose a stack is to be implemented with a linked list instead of an array. What would be the
effect on the time complexity of the push and pop operations of the stack implemented using
linked list (Assuming stack is implemented efficiently)?
a) O(1) for insertion and O(n) for deletion
b) O(1) for insertion and O(1) for deletion
c) O(n) for insertion and O(1) for deletion
d) O(n) for insertion and O(n) for deletion


8) Consider n elements that are equally distributed in k stacks. In each stack, elements of it are
arranged in ascending order (min is at the top in each of the stack and then increasing
downwards). Given a queue of size n in which we have to put all n elements in increasing
order. What will be the time complexity of the best known algorithm?
a) O(n)
b) O(n 2 )
c) O(n log (n))
d) O(nk)


9) The data structure required to check whether an expression contains balanced parenthesis is?
a) Stack
b) Queue
c) Array
d) Tree


10) The process of accessing data stored in a serial access memory is similar to manipulating data
on a ______?
a) Heap
b) Binary Tree
c) Array
d) Stack


11) Which data structure is needed to convert infix notation to postfix notation?
a) Branch
b) Tree
c) Queue
d) Stack

12) What is the result of the following operation



Top (Push (S, X))
a) X
b) Null
c) S
d) None


13) Which data structure is used for implementing recursion?


a) Queue
b) Stack
c) Array
d) List


14) Which of the following statement(s) about stack data structure is/are NOT correct?
a) Stack data structure can be implemented using linked list
b) New node can only be added at the top of the stack
c) Stack is the FIFO data structure
d) The last node at the bottom of the stack has a NULL link


15) Consider the linked list implementation of a stack. Which of the following node is considered as
Top of the stack?
a) First node
b) Last node
c) Any node
d) Middle node


16) Consider the following operation performed on a stack of size 5.



Push(1);

Pop();

Push(2);

Push(3);

Pop();

Push(4);

Pop();

Pop();

Push(5);

After the completion of all operation, the no of element present on stack are
a) 1
b) 2
c) 3
d) 4

17) Which of the following is not an inherent application of stack?


a) Reversing a string.
b) Evaluation of postfix expression.
c) Implementation of recursion.
d) Job scheduling.


18) Which of the following operation take worst case linear time in the array implementation of
stack?
a) Push
b) Pop
c) IsEmpty
d) None


19) Which of the following application generally use a stack?


a) Parenthesis balancing program
b) Syntax analyzer in compiler
c) Keeping track of local variables at run time
d) All of the above


20) What is the minimum number of stacks of size n required to implement a queue of size n?
a) One
b) Two
c) Three
d) Four


Programming [stack]:-
1) Write a program to Implement stack using queues. You have operations of queue (enqueue,
dequeue) and you have to implement operations of stack (push, pop, etc…)
2) Write a program to Implement stack using array.
3) Write a program to Implement stack using linked list.
4) Write a program to convert a given infix expression to post fix expression.
5) Write a program to reverse a stack using recursion.


Queue
1) A Queue is best characterized as 

a) Last In First Out

b) First In Last Out

c) First In First Out 

d) None of the above


2) Given an empty queue Q, what does it look like after the following operations?


Q.enqueue(5)
Q.enqueue(2)
Q.dequeue()
Q.enqueue(3)
Q.dequeue()
a) 3 
b) 5 
c) 9 
d) none of the above

3) Given a 5 element stack S (from top to bottom: 2, 4, 6, 8, 10), and an empty queue Q, remove
the elements one-by-one from S and insert them into Q, then remove them one-by-one from Q
and re-insert them into S. S now looks like (from top to bottom). 
a) 2, 4, 6, 8, 10 
b) 10, 2, 4, 6, 8 
c) 10, 8, 6, 4, 2 
d) none of the above


4) What is the reason for using a "circular queue" instead of a regular one? 
a) the running time of enqueue() is improved.
b) reuse empty spaces.
c) you can traverse all the elements more efficiently.
d) None of the above.

5) Given an efficient circular array-based queue Q capable of holding 10 objects. Show the final
contents of the array after the following code is executed:
for (int k = 1; k <= 7; k++)
Q.enqueue(k);
for (int k = 1; k <= 4; k++)
Q.enqueue(Q.dequeue());


Final Answer:


6) The stack data type is restrictive in a sense that you cannot

a) remove the top element

b) insert at the top

c) look at the top element

d) remove the bottom element


7) Assume that you have an empty circular queue (array-based implementation) which can
hold only four elements. What are the array indexes of the back and the front elements
after executing this series of queue operations? (indexes start with 0)

enqueue(“a”), enqueue(“b”), getFront(), enqueue(“c”), enqueue(“d”), dequeue(), dequeue(),
dequeue(), enqueue(“a”), enqueue(“b”), enqueue(“c”), dequeue(), getFront(), enqueue(“d”),
dequeue()


front index = ____________ back index = ________________


8) Which one of the following is an application of Queue Data Structure?

a) When a resource is shared among multiple consumers.

b) When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes

c) Load Balancing

d) All of the above


9) 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.

a) 1

b) 2

c) 3

d) 4


10) 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.

a) Array

b) Linked List

c) Heap Data Structures like Binary Heap, Fibonacci Heap

d) None of the above


11) Which of the following is true about linked list implementation of queue?

a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.

b) In push operation, if new nodes are inserted at the end, then in pop operation, nodes
must be removed from the beginning.

c) Both of the above

d) None of the above


12) Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n


elements. Assume that the insertion and deletion operation are carried out using REAR
and FRONT as array index variables, respectively. Initially, REAR = FRONT = 0. The
conditions to detect queue full and queue empty are

a) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT

b) Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR

c) Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT

d) Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT


13) 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:

a) 10, 8, 7, 5, 3, 2, 1

b) 10, 8, 7, 2, 3, 1, 5

c) 10, 8, 7, 1, 2, 3, 5

d) 10, 8, 7, 3, 2, 1, 5

14) 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) Θ(n)

b) Θ(n + k)

c) Θ(n * k)

d) Θ(n 2 )

15) Suppose you are given an implementation of a queue of integers. The operations that can
be performed on the queue are: 
1) isEmpty (Q) — returns true if the queue is empty, false otherwise. 
2) delete (Q) — deletes the element at the front of the queue and returns its value. 
c) insert (Q, i) — inserts the integer i at the rear of the queue. 

Consider the following function:

void f (queue Q) {

int i ;

if (!isEmpty(Q)) {

i = delete(Q);

f(Q);

insert(Q, i);

}

}


What operation is performed by the above function f ?
a) Leaves the queue Q unchanged
b) Reverses the order of the elements in the queue Q
c) Deletes the element at the front of the queue Q and inserts it at the rear keeping the
other elements in the same order
d) Empties the queue Q


16) The data structure required for Breadth First Traversal on a graph is?
a) Stack
b) Array
c) Queue 
d) Tree


17) aIn linked list implementation of a queue, where does a new element be inserted?
a) At the head of link list
b) At the tail of the link list 
c) At the centre position in the link list
d) None
18) In linked list implementation of queue, if only front pointer is maintained, which of the following
operation take worst case linear time?
a) Insertion
b) Deletion
c) To empty a queue
d) Both a) and c) 


19) A data structure in which elements can be inserted or deleted at/from both the ends but not in
the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue


Programming [Queue]
1) Write a program to implement queue using stacks. You have only stack operations push, pop
and you need to implement queue operation like enqueue dequeue etc.
2) Write a program to Implement a queue data structure using array.
3) Write a program to Implement a queue data structure using linked list.
4) Write a program to Implement min-heap using array. implement create heap, delete min, insert
element etc..
5) Write a program to check whether a given Binary Tree is Complete or not. for this you need to
use Breadth first search using queue.

You might also like