Dsa Notes
Dsa Notes
21\. Which of the following is the most widely used external memory data structure?
a) B-tree
b) Red-black tree
c) AVL tree
d) Both AVL tree and Red-black tree
Answer: a
Explanation: In external memory, the data is transferred in form of blocks. These
blocks have data valued and pointers. And B-tree can hold both the data values and
pointers. So B-tree is used as an external memory data structure.
22\. Which of the following is also known as Rope data structure?
a) Linked List
b) Array
c) String
d) Cord
Answer: d
Explanation: Array is a linear data structure. Strings are a collection and
sequence of codes, alphabets or characters. Linked List is a linear data structure
having a node containing data input and the address of the next node. The cord is
also known as the rope data structure.
24\. Which of the following data structure can provide efficient searching of the
elements?
a) binary search tree
b) unordered lists
c) 2-3 tree
d) treap
Answer: c
Explanation: The average case time for lookup in a binary search tree, treap and
2-3 tree is O(log n) and in unordered lists it is O(n). But in the worst case, only
the 2-3 trees perform lookup efficiently as it takes O(log n), while others take
O(n).
25\. What is an AVL tree?
a) a tree which is unbalanced and is a height balanced tree
b) a tree which is balanced and is a height balanced tree
c) a tree with atmost 3 children
d) a tree with three children
Answer: b
Explanation: It is a self balancing tree with height difference atmost 1.
29\. Which is the most appropriate data structure for reversing a word?
a) stack
b) queue
c) graph
d) tree
Answer: a
32\. What is the advantage of a hash table as a data structure?
a) easy to implement
b) faster access of data
c) exhibit good locality of reference
d) very efficient for less number of entries
Answer: b
Explanation: Hash table is a data structure that has an advantage that it allows
fast access of elements. Hash functions are used to determine the index of any
input record in a hash table
33\. Which type of data structure is a ternary heap?
a) Hash
b) Array
c) Priority Stack
d) Priority Queue
Answer: d
Explanation: Ternary heap is a type of data structure in the field of computer
science. It is a part of the Heap data structure family. It is a priority queue
type of data structure that follows all the property of heap.
34\. What is a dequeue?
a) A queue implemented with both singly and doubly linked lists
b) A queue with insert/delete defined for front side of the queue
c) A queue with insert/delete defined for both front and rear ends of the queue
d) A queue implemented with a doubly linked list
Answer: c
Explanation: A dequeue or a double ended queue is a queue with insert/delete
defined for both front and rear ends of the queue.
35\. A data structure in which elements can be inserted or deleted at/from both
ends but not in the middle is?
a) Priority queue
b) Dequeue
c) Circular queue
d) Queue
Answer: b
-----------------------------------------------------------------------------------
--------
Data Structure Questions and Answers – Array and Array Operations
-----------------------------------------------------------------------------------
---------
-----------------------------------------------------------------------------------
-
Data Structure Multiple Choice Questions – Stack
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
Data Structure Questions and Answers – Queue Operations
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------
Data Structure Questions and Answers – Singly Linked List Operations –
1
-----------------------------------------------------------------------------------
-----------------
1\. A linear collection of data elements where the linear node is given by means of
pointer is called?
a) Linked list
b) Node list
c) Primitive list
d) Unordered list
Answer: a
2\. Consider an implementation of unsorted singly linked list. Suppose it has its
representation with a head pointer only. Given the representation, which of the
following operation can be implemented in O(1) time?
i) Insertion at the front of the linked list
ii) Insertion at the end of the linked list
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list
a) I and II
b) I and III
c) I, II and III
d) I, II and IV
Answer: b
Explanation: We know the head node in the given linked list. Insertion and deletion
of elements at the front of the linked list completes in O (1) time whereas for
insertion and deletion at the last node requires to traverse through every node in
the linked list. Suppose there are n elements in a linked list, we need to traverse
through each node. Hence time complexity becomes O(n).
3\. In linked list each node contains a minimum of two fields. One field is data
field to store the data second field is?
a) Pointer to character
b) Pointer to integer
c) Pointer to node
d) Node
Answer: c
4\. What would be the asymptotic time complexity to add a node at the end of singly
linked list, if the pointer is initially pointing to the head of the list?
a) O(1)
b) O(n)
c) θ(n)
d) θ(1)
Answer: c
Explanation: In case of a linked list having n elements, we need to travel through
every node of the list to add the element at the end of the list. Thus asymptotic
time complexity is θ(n).
7. What would be the asymptotic time complexity to insert an element at the second
position in the linked list?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: a
8. The concatenation of two lists can be performed in O(1) time. Which of the
following variation of the linked list can be used?
a) Singly linked list
b) Doubly linked list
c) Circular doubly linked list
d) Array implementation of list
Answer: c
Explanation: We can easily concatenate two lists in O (1) time using singly or
doubly linked list, provided that we have a pointer to the last node at least one
of the lists. But in case of circular doubly linked lists, we will break the link
in both the lists and hook them together. Thus circular doubly linked list
concatenates two lists in O (1) time.
9. Consider the following definition in c programming language.
------------------------------------------------
struct node
{
int data;
struct node * next;
}
typedef struct node NODE;
NODE *ptr;
--------------------------------------------------
Which of the following c code is used to create new node?
a) ptr = (NODE*)malloc(sizeof(NODE));
b) ptr = (NODE*)malloc(NODE);
c) ptr = (NODE*)malloc(sizeof(NODE*));
d) ptr = (NODE)malloc(sizeof(NODE));
Answer: a
Explanation: As it represents the right way to create a node.
1\. What kind of linked list is best to answer questions like “What is the item at
position n?”
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Array implementation of linked list
Answer: d
Explanation: Arrays provide random access to elements by providing the index value
within square brackets. In the linked list, we need to traverse through each
element until we reach the nth position. Time taken to access an element
represented in arrays is less than the singly, doubly and circular linked lists.
Thus, array implementation is used to access the item at the position n.
2\. Linked lists are not suitable for the implementation of \_\_\_\_\_\_\_\_\_\_\_
a) Insertion sort
b) Radix sort
c) Polynomial manipulation
d) Binary search
Answer: d
Explanation: It cannot be implemented using linked lists.
3\. Linked list is considered as an example of \_\_\_\_\_\_\_\_\_\_\_ type of
memory allocation.
a) Dynamic
b) Static
c) Compile time
d) Heap
Answer: a
Explanation: As memory is allocated at the run time.
4\. In Linked List implementation, a node carries information regarding \_\_\_\_\_\
_\_\_\_\_\_
a) Data
b) Link
c) Data and Link
d) Node
Answer: c
5\. Linked list data structure offers considerable saving in \_\_\_\_\_\_\_\_\_\_\
_\_\_
a) Computational Time
b) Space Utilization
c) Space Utilization and Computational Time
d) Speed Utilization
Answer: c
Explanation: Linked lists saves both space and time.
6\. Which of the following points is/are not true about Linked List data structure
when it is compared with an array?
a) Arrays have better cache locality that can make them better in terms of
performance
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked Lists
d) Access of elements in linked list takes less time than compared to arrays
Answer: d
7\. What does the following function do for a given Linked List with first node as
head?
void fun1(struct node\* head)
{
if(head == NULL) return;
fun1(head->next);
printf("%d ", head->data);
}
a) Prints all nodes of linked lists
b) Prints all nodes of linked list in reverse order
c) Prints alternate nodes of Linked List
d) Prints alternate nodes in reverse order
Answer: b
8\. Which of the following sorting algorithms can be used to sort a random linked
list with minimum time complexity?
a) Insertion Sort
b) Quick Sort
c) Heap Sort
d) Merge Sort
Answer: d
Explanation: Both Merge sort and Insertion sort can be used for linked lists. The
slow random-access performance of a linked list makes other algorithms (such as
quicksort) perform poorly, and others (such as heapsort) completely impossible.
Since worst case time complexity of Merge Sort is O(nLogn) and Insertion sort is
O(n2), merge sort is preferred.
2\. What is the output of following function for start pointing to first node of
following linked list?
1->2->3->4->5->6
void fun(struct node\* start)
{
if(start == NULL) return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
c) 1 2 3 5
d) 1 3 5 5 3 1
Answer: d
Explanation: fun() prints alternate nodes of the given Linked List, first from
head to end, and then from end to head.
If Linked List has even number of nodes, then skips the last node.
5\. In the worst case, the number of comparisons needed to search a singly linked
list of length n for a given element is?
a) log 2 n
b) n⁄2
c) log 2 n – 1
d) n
Answer: d
6\. Given pointer to a node X in a singly linked list. Only one pointer is given,
pointer to head node is not given, can we delete the node X from given linked list?
a) Possible if X is not last node
b) Possible if size of linked list is even
c) Possible if size of linked list is odd
d) Possible if X is not first node
struct node \*temp = X->next; X->data = temp->data; X->next = temp->next;
free(temp);
7\. You are given pointers to first and last nodes of a singly linked list, which
of the following operations are dependent on the length of the linked list?
a) Delete the first element
b) Insert a new element as a first element
c) Delete the last element of the list
d) Add a new element at the end of the list
Answer: c
Explanation: Deletion of the first element of the list is done in O (1) time by
deleting memory and changing the first pointer.
Insertion of an element as a first element can be done in O (1) time. We will
create a node that holds data and points to the head of the given linked list. The
head pointer was changed to a newly created node.
Deletion of the last element requires a pointer to the previous node of last,
which can only be obtained by traversing the list. This requires the length of the
linked list.
Adding a new element at the end of the list can be done in O (1) by changing the
pointer of the last node to the newly created node and last is changed to a newly
created node.
2\. What is the time complexity of inserting at the end in dynamic arrays?
a) O(1)
b) O(n)
c) O(logn)
d) Either O(1) or O(n)
Answer: d
Explanation: Depending on whether the array is full or not, the complexity in
dynamic array varies. If you try to insert into an array that is not full, then the
element is simply stored at the end, this takes O(1) time. If you try to insert
into an array which is full, first you will have to allocate an array with double
the size of the current array and then copy all the elements into it and finally
insert the new element, this takes O(n) time.
6\. What is the space complexity for deleting a linked list?
a) O(1)
b) O(n)
c) Either O(1) or O(n)
d) O(logn)
Answer: a
Explanation: You need a temp variable to keep track of current node, hence the
space complexity is O(1).
8\. Which of these is not an application of a linked list?
a) To implement file systems
b) For separate chaining in hash-tables
c) To implement non-binary trees
d) Random Access of elements
Answer: d
Explanation: To implement file system, for separate chaining in hash-tables and to
implement non-binary trees linked lists are used. Elements are accessed
sequentially in linked list. Random access of elements is not an applications of
linked list.
-----------------------------------------------------------------------------------
-----------------
Data Structure Questions and Answers – Doubly Linked List
-----------------------------------------------------------------------------------
-----------------
1\. Which of the following is false about a doubly linked list?
a) We can navigate in both the directions
b) It requires more space than a singly linked list
c) The insertion and deletion of a node take a bit longer
d) Implementing a doubly linked list is easier than singly linked list
Answer: d
Explanation: A doubly linked list has two pointers ‘left’ and ‘right’ which enable
it to traverse in either direction. Compared to singly liked list which has only a
‘next’ pointer, doubly linked list requires extra space to store this extra
pointer. Every insertion and deletion requires manipulation of two pointers, hence
it takes a bit longer time. Implementing doubly linked list involves setting both
left and right pointers to correct nodes and takes more time than singly linked
list.
3\. What is a memory efficient double linked list?
a) Each node has only one pointer to traverse the list back and forth
b) The list has breakpoints for faster traversal
c) An auxiliary singly linked list acts as a helper list to traverse through the
doubly linked list
d) A doubly linked list that uses bitwise AND operator for storing addresses
Answer: a
Explanation: Memory efficient doubly linked list has only one pointer to traverse
the list back and forth. The implementation is based on pointer difference. It uses
bitwise XOR operator to store the front and rear pointer addresses. Instead of
storing actual memory address, every node store the XOR address of previous and
next nodes.
5\. How do you calculate the pointer difference in a memory efficient double linked
list?
a) head xor tail
b) pointer to previous node xor pointer to next node
c) pointer to previous node – pointer to next node
d) pointer to next node – pointer to previous node
Answer: b
Explanation: The pointer difference is calculated by taking XOR of pointer to
previous node and pointer to the next node.
6\. What is the worst case time complexity of inserting a node in a doubly linked
list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)
Answer: c
Explanation: In the worst case, the position to be inserted maybe at the end of
the list, hence you have to traverse through the entire list to get to the correct
position, hence O(n).
8\. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be
the list after performing the given sequence of operations?
Node temp = new Node(6,head,head.getNext()); Node temp1 = new
Node(0,tail.getPrev(),tail); head.setNext(temp); temp.getNext().setPrev(temp);
tail.setPrev(temp1); temp1.getPrev().setNext(temp1);
a) head-0-1-2-3-4-5-6-tail
b) head-1-2-3-4-5-6-tail
c) head-6-1-2-3-4-5-0-tail
d) head-0-1-2-3-4-5-tail
View Answer
Answer: c
Explanation: The given sequence of operations performs addition of nodes at the
head and tail of the list.
9\. What is the functionality of the following piece of code?
public int function() { Node temp = tail.getPrev(); tail.setPrev(temp.getPrev());
temp.getPrev().setNext(tail); size--; return temp.getItem(); }
a) Return the element at the tail of the list but do not remove it
b) Return the element at the tail of the list and remove it from the list
c) Return the last but one element from the list but do not remove it
d) Return the last but one element at the tail of the list and remove it from the
list
Answer: b
Explanation: The previous and next pointers of the tail and the last but one
element are manipulated, this suggests that the last node is being removed from the
list.
10\. Consider the following doubly linked list: head-1-2-3-4-5-tail. What will be
the list after performing the given sequence of operations?
Node temp = new Node(6,head,head.getNext()); head.setNext(temp);
temp.getNext().setPrev(temp); Node temp1 = tail.getPrev();
tail.setPrev(temp1.getPrev()); temp1.getPrev().setNext(tail);
a) head-6-1-2-3-4-5-tail
b) head-6-1-2-3-4-tail
c) head-1-2-3-4-5-6-tail
d) head-1-2-3-4-5-tail
Answer: b
Explanation: A new node is added to the head of the list and a node is deleted
from the tail end of the list.
-----------------------------------------------------------------------------------
-------------------------
Data Structure Questions and Answers – Circular Linked List
-----------------------------------------------------------------------------------
-------------------------
1\. What differentiates a circular linked list from a normal linked list?
a) You cannot have the ‘next’ pointer point to null in a circular linked list
b) It is faster to traverse the circular linked list
c) In a circular linked list, each node points to the previous node instead of the
next node
d) Head node is known in circular linked list
Answer: a
Explanation: In a normal linked list, the ‘next’ pointer of the last node points
to null. However, in a circular linked list, the ‘next’ pointer of the last node
points to the head (first element) of the list. Every node in a circular linked
list can be a starting point(head).
4\. What is the time complexity of searching for an element in a circular linked
list?
a) O(n)
b) O(nlogn)
c) O(1)
d) O(n2)
Answer: a
5\. Which of the following application makes use of a circular linked list?
a) Undo operation in a text editor
b) Recursive function calls
c) Allocating CPU to resources
d) Implement Hash Tables
Answer: c
Explanation: Generally, round robin fashion is employed to allocate CPU time to
resources which makes use of the circular linked list data structure. Recursive
function calls use stack data structure. Undo Operation in text editor uses doubly
linked lists. Hash tables uses singly linked lists.
9\. Which of the following is false about a circular linked list?
a) Every node has a successor
b) Time complexity of inserting a new node at the head of the list is O(1)
c) Time complexity for deleting the last node is O(n)
d) We can traverse the whole circular linked list by starting from any point
Answer: b
Explanation: Time complexity of inserting a new node at the head of the list is
O(n) because you have to traverse through the list to find the tail node.
10\. Consider a small circular linked list. How to detect the presence of cycles in
this list effectively?
a) Keep one node as head and traverse another temp node till the end to check if
its ‘next points to head
b) Have fast and slow pointers with the fast pointer advancing two nodes at a time
and slow pointer advancing by one node at a time
c) Cannot determine, you have to pre-define if the list contains cycles
d) Circular linked list itself represents a cycle. So no new cycles cannot be
generated
Answer: b
Explanation: Advance the pointers in such a way that the fast pointer advances two
nodes at a time and slow pointer advances one node at a time and check to see if at
any given instant of time if the fast pointer points to slow pointer or if the fast
pointer’s ‘next’ points to the slow pointer. This is applicable for smaller lists.
-------------------------------------------------------------------------------
Data Structure Questions and Answers – Stack using Array
---------------------------------------------------------------------------------
2\. What does the following function check for? (all necessary headers to be
included and function is called from main)
)
a) full stack
b) invalid index
c) empty stack
d) infinite stack
Answer: c
5\. What is the time complexity of pop() operation when the stack is implemented
using an array?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)
Answer: a
Explanation: pop() accesses only one end of the structure, and hence constant time.
6\. Which of the following array position will be occupied by a new element being
pushed for a stack of size N elements(capacity of stack > N)?
a) S[N-1]
b) S[N]
c) S[1]
d) S[0]
Answer: b
Explanation: Elements are pushed at the end, hence N.
7\. What happens when you pop from an empty stack while implementing using the
Stack ADT in Java?
a) Undefined error
b) Compiler displays a warning
c) EmptyStackException is thrown
d) NoStackException is thrown
Answer: c
Explanation: The Stack ADT throws an EmptyStackException if the stack is empty and
a pop() operation is tried on it.
9\. Array implementation of Stack is not dynamic, which of the following statements
supports this argument?
a) space allocation for array is fixed and cannot be changed during run-time
b) user unable to give the input for stack operations
c) a runtime exception halts execution
d) improper program compilation
Answer: a
Explanation: You cannot modify the size of an array once the memory has been
allocated, adding fewer elements than the array size would cause wastage of space,
and adding more elements than the array size at run time would cause Stack
Overflow.
10\. Which of the following array element will return the top-of-the-stack-element
for a stack of size N elements(capacity of stack > N)?
a) S[N-1]
b) S[N]
c) S[N-2]
d) S[N+1]
Answer: a
-----------------------------------------------------------------------------------
-----------------------------
Data Structure Questions and Answers – Stack using Linked List
-----------------------------------------------------------------------------------
-------------------------------
2\. Which of the following statements are not correct with respect to Singly Linked
List(SLL) and Doubly Linked List(DLL)?
a) Complexity of Insertion and Deletion at known position is O(n) in SLL and O(1)
in DLL
b) SLL uses lesser memory per node than DLL
c) DLL has more searching power than SLL
d) Number of node fields in SLL is more than DLL
Answer: d
4\. What does the following function do?
public Object some\_func()throws emptyStackException { if(isEmpty()) throw new
emptyStackException("underflow"); return first.getEle(); }
a) pop
b) delete the top-of-the-stack element
c) retrieve the top-of-the-stack element
d) push operation
Answer: c
6\. What does ‘stack overflow’ refer to?
a) accessing item from an undefined stack
b) adding items to a full stack
c) removing items from an empty stack
d) index out of bounds exception
Answer: b
9\. Which of the following data structures can be used for parentheses matching?
a) n-ary tree
b) queue
c) priority queue
d) stack
Answer: d
10\. Minimum number of queues to implement stack is \_\_\_\_\_\_\_\_\_\_\_
a) 3
b) 4
c) 1
d) 2
Answer: c
Explanation: Use one queue and one counter to count the number of elements in the
queue.
-----------------------------------------------------------------------------------
-------------------
Data Structure Questions and Answers – Queue using Array
-----------------------------------------------------------------------------------
---------------------
1\. Which of the following properties is associated with a queue?
a) First In Last Out
b) First In First Out
c) Last In First Out
d) Last In Last Out
Answer: b
Explanation: Queue follows First In First Out structure.
2\. In a circular queue, how do you increment the rear end of the queue?
a) rear++
b) (rear+1) % CAPACITY
c) (rear % CAPACITY)+1
d) rear–
Answer: b
Explanation: Ensures rear takes the values from 0 to (CAPACITY-1).
3\. What is the term for inserting into a full queue known as?
a) overflow
b) underflow
c) null pointer exception
d) program won’t be compiled
Answer: a
Explanation: Just as stack, inserting into a full queue is termed overflow.
4\. What is the time complexity of enqueue operation?
a) O(logn)
b) O(nlogn)
c) O(n)
d) O(1)
Answer: d
6\. What is the need for a circular queue?
a) effective usage of memory
b) easier computations
c) to delete elements based on priority
d) implement LIFO principle in queues
Answer: a
9\. What is the space complexity of a linear queue having n elements?
a) O(n)
b) O(nlogn)
c) O(logn)
d) O(1)
Answer: a
-----------------------------------------------------------------------------------
----
Data Structure Questions and Answers – Queue using Linked List
-----------------------------------------------------------------------------------
-----
-----------------------------------------------------------------------------------
-----------------------------
Data Structure Questions and Answers – Priority Queue
-----------------------------------------------------------------------------------
-----------------------------
1\. With what data structure can a priority queue be implemented?
a) Array
b) List
c) Heap
d) Tree
Answer: c
Explanation: Priority queue can be implemented using an array, a list, a binary
search tree or a heap, although the most efficient one being the heap.
2\. Which of the following is not an application of priority queue?
a) Huffman codes
b) Interrupt handling in operating system
c) Undo operation in text editors
d) Bayesian spam filter
Answer: c
Explanation: Undo operation is achieved using a stack.
4\. What is the time complexity to insert a node based on key in a priority queue?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: c
Explanation: In the worst case, you might have to traverse the entire list.
6\. What is not a disadvantage of priority scheduling in operating systems?
a) A low priority process might have to wait indefinitely for the CPU
b) If the system crashes, the low priority systems may be lost permanently
c) Interrupt handling
d) Indefinite blocking
Answer: c
Explanation: The lower priority process should wait until the CPU completes the
processing higher priority process. Interrupt handling is an advantage as
interrupts should be given more priority than tasks at hand so that interrupt can
be serviced to produce desired results.
7\. Which of the following is not an advantage of a priority queue?
a) Easy to implement
b) Processes with different priority can be efficiently handled
c) Applications with differing requirements
d) Easy to delete elements in any case
View Answer
Answer: d
Explanation: In worst case, the entire queue has to be searched for the element
having the highest priority. This will take more time than usual. So deletion of
elements is not an advantage.
8\. What is the time complexity to insert a node based on position in a priority
queue?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: c
Explanation: In the worst case, you might have to traverse the entire list.
-----------------------------------------------------------------------------------
-----------------------------------
Data Structure Questions and Answers – Double Ended Queue (Dequeue)
-----------------------------------------------------------------------------------
-----------------------------------
1\. What is a dequeue?
a) A queue with insert/delete defined for both front and rear ends of the queue
b) A queue implemented with a doubly linked list
c) A queue implemented with both singly and doubly linked lists
d) A queue with insert/delete defined for front side of the queue
Answer: a
Explanation: A dequeue or a double ended queue is a queue with insert/delete
defined for both front and rear ends of the queue.
4\. What are the applications of dequeue?
a) A-Steal job scheduling algorithm
b) Can be used as both stack and queue
c) To find the maximum of all sub arrays of size k
d) All of the mentioned
Answer: d
Explanation: All of the mentioned can be implemented with a dequeue.
7\. What is the time complexity of deleting from the rear end of the dequeue
implemented with a singly linked list?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: c
Explanation: Since a singly linked list is used, first you have to traverse till
the end, so the complexity is O(n).
8\. After performing these set of operations, what does the final list look
contain?
InsertFront(10); InsertFront(20); InsertRear(30); DeleteFront(); InsertRear(40);
InsertRear(10); DeleteRear(); InsertRear(15); display();
a) 10 30 10 15
b) 20 30 40 15
c) 20 30 40 10
d) 10 30 40 15
Answer: d
-----------------------------------------------------------------------------------
----------------------------------------------------
Data Structure Questions and Answers – Queue using Stacks
-----------------------------------------------------------------------------------
------------------------------------------------------
1\. A Double-ended queue supports operations such as adding and removing items from
both the sides of the queue. They support four operations like addFront(adding item
to top of the queue), addRear(adding item to the bottom of the queue),
removeFront(removing item from the top of the queue) and removeRear(removing item
from the bottom of the queue). You are given only stacks to implement this data
structure. You can implement only push and pop operations. What are the total
number of stacks required for this operation?(you can reuse the stack)
a) 1
b) 2
c) 3
d) 4
Answer: b
2\. You are asked to perform a queue operation using a stack. Assume the size of
the stack is some value ‘n’ and there are ‘m’ number of variables in this stack.
The time complexity of performing deQueue operation is (Using only stack operations
like push and pop)(Tightly bound).
a) O(m)
b) O(n)
c) O(m\*n)
d) Data is insufficient
Answer: a
Explanation: To perform deQueue operation you need to pop each element from the
first stack and push it into the second stack. In this case you need to pop ‘m’
times and need to perform push operations also ‘m’ times. Then you pop the first
element from this second stack (constant time) and pass all the elements to the
first stack (as done in the beginning)(‘m-1’ times). Therfore the time complexity
is O(m).
3\. Consider you have an array of some random size. You need to perform dequeue
operation. You can perform it using stack operation (push and pop) or using queue
operations itself (enQueue and Dequeue). The output is guaranteed to be same. Find
some differences?
a) They will have different time complexities
b) The memory used will not be different
c) There are chances that output might be different
d) No differences
Answer: a
Explanation: To perform operations such as Dequeue using stack operation you need
to empty all the elements from the current stack and push it into the next stack,
resulting in a O(number of elements) complexity whereas the time complexity of
dequeue operation itself is O(1). And there is a need of a extra stack. Therefore
more memory is needed.
4\. Consider you have a stack whose elements in it are as follows.
5 4 3 2 << top
Where the top element is 2.
You need to get the following stack
6 5 4 3 2 << top
The operations that needed to be performed are (You can perform only push and
pop):
a) Push(pop()), push(6), push(pop())
b) Push(pop()), push(6)
c) Push(pop()), push(pop()), push(6)
d) Push(6)
Answer: a
Explanation: By performing push(pop()) on all elements on the current stack to the
next stack you get 2 3 4 5 << top.Push(6) and perform push(pop()) you’ll get back 6
5 4 3 2 << top. You have actually performed enQueue operation using push and pop.
5\. A double-ended queue supports operations like adding and removing items from
both the sides of the queue. They support four operations like addFront(adding item
to top of the queue), addRear(adding item to the bottom of the queue),
removeFront(removing item from the top of the queue) and removeRear(removing item
from the bottom of the queue). You are given only stacks to implement this data
structure. You can implement only push and pop operations. What’s the time
complexity of performing addFront and addRear? (Assume ‘m’ to be the size of the
stack and ‘n’ to be the number of elements)
a) O(m) and O(n)
b) O(1) and O(n)
c) O(n) and O(1)
d) O(n) and O(m)
Answer: b
Explanation: addFront is just a normal push operation. Push operation is of O(1).
Whereas addRear is of O(n) as it requires two push(pop()) operations of all
elements of a stack.
6\. Why is implementation of stack operations on queues not feasible for a large
dataset (Asssume the number of elements in the stack to be n)?
a) Because of its time complexity O(n)
b) Because of its time complexity O(log(n))
c) Extra memory is not required
d) There are no problems
Answer: a
Explanation: To perform Queue operations such as enQueue and deQueue there is a
need of emptying all the elements of a current stack and pushing elements into the
next stack and vice versa. Therfore it has a time complexity of O(n) and the need
of extra stack as well, may not be feasible for a large dataset.
7\. Consider yourself to be in a planet where the computational power of chips to
be slow. You have an array of size 10.You want to perform enqueue some element into
this array. But you can perform only push and pop operations .Push and pop
operation both take 1 sec respectively. The total time required to perform enQueue
operation is?
a) 20
b) 40
c) 42
d) 43
Answer: d
Explanation: First you have to empty all the elements of the current stack into
the temporary stack, push the required element and empty the elements of the
temporary stack into the original stack. Therfore taking 10+10+1+11+11= 43 seconds.
8\. You have two jars, one jar which has 10 rings and the other has none. They are
placed one above the other. You want to remove the last ring in the jar. And the
second jar is weak and cannot be used to store rings for a long time.
a) Empty the first jar by removing it one by one from the first jar and placing it
into the second jar
b) Empty the first jar by removing it one by one from the first jar and placing it
into the second jar and empty the second jar by placing all the rings into the
first jar one by one
c) There exists no possible way to do this
d) Break the jar and remove the last one
Answer: b
9\. Given only a single array of size 10 and no other memory is available. Which of
the following operation is not feasible to implement (Given only push and pop
operation)?
a) Push
b) Pop
c) Enqueue
d) Returntop
Answer: c
Explanation: To perform Enqueue using just push and pop operations, there is a
need of another array of same size. But as there is no extra available memeory, the
given operation is not feasible.
10\. Given an array of size n, let’s assume an element is ‘touched’ if and only if
some operation is performed on it(for example, for performing a pop operation the
top element is ‘touched’). Now you need to perform Dequeue operation. Each element
in the array is touched atleast?
a) Once
b) Twice
c) Thrice
d) Four times
Answer: d
Explanation: First each element from the first stack is popped, then pushed into
the second stack, dequeue operation is done on the top of the stack and later the
each element of second stack is popped then pushed into the first stack. Therfore
each element is touched four times.
-----------------------------------------------------------------------------------
--------------------
Data Structure Questions and Answers – Stack using Queues
-----------------------------------------------------------------------------------
----------------------
1\. To implement a stack using queue(with only enqueue and dequeue operations), how
many queues will you need?
a) 1
b) 2
c) 3
d) 4
Answer: b
public void fun(int x) { q1.offer(x); }
a) Perform push() with push as the costlier operation
b) Perform push() with pop as the costlier operation
c) Perform pop() with push as the costlier operation
d) Perform pop() with pop as the costlier operation
Answer: b
Explanation: offer() suggests that it is a push operation, but we see that it is
performed with only one queue, hence the pop operation is costlier.
===================================================================================
=============================================
```````````````````````````````````````````````````````````````````````````````````
`````````````````````````````````````````````
APPLICATION OF STACK
-----------------------------------------------------------------------------------
----------------------------------------------
===================================================================================
=============================================
1\. How many stacks are required for applying evaluation of infix expression
algorithm?
a) one
b) two
c) three
d) four
Answer: b
Explanation: Two stacks are required for evaluation of infix expression – one for
operands and one for operators.
2\. How many passes does the evaluation of infix expression algorithm makes through
the input?
a) One
b) Two
c) Three
d) Four
Answer: a
Explanation: Evaluation of infix expression algorithm is linear and makes only one
pass through the input.
4\. Which of the following statement is incorrect with respect to evaluation of
infix expression algorithm?
a) Operand is pushed on to the stack
b) If the precedence of operator is higher, pop two operands and evaluate
c) If the precedence of operator is lower, pop two operands and evaluate
d) The result is pushed on to the operand stack
Answer: b
Explanation: If the precedence of the operator is higher than the stack operator,
then it is pushed on to the stack operator.
6\. Evaluation of infix expression is done based on precedence of operators.
a) True
b) False
Answer: a
7\. Of the following choices, which operator has the lowest precedence?
a) ^
b) +
c) /
d) #
Answer: d
8\. The system throws an error if parentheses are encountered in an infix
expression evaluation algorithm.
a) True
b) False
Answer: b
1\. How many stacks are required for evaluation of prefix expression?
a) one
b) two
c) three
d) four
Answer: b
Explanation: 2 stacks are required for evaluation of prefix expression, one for
integers and one for characters.
2\. While evaluating a prefix expression, the string is read from?
a) left to right
b) right to left
c) center to right
d) center to left to right
Answer: b
Explanation: The string is read from right to left because a prefix string has
operands to its right side.
3\. The associativity of an exponentiation operator ^ is right side.
a) True
b) False
Answer: a
Explanation: The associativity of ^ is right side while the rest of the operators
like +,-,\*,/ has its associativity to its left.
4\. How many types of input characters are accepted by this algorithm?
a) one
b) two
c) three
d) four
Answer: c
Explanation: Three kinds of input are accepted by this algorithm- numbers,
operators and new line characters.
5\. What determines the order of evaluation of a prefix expression?
a) precedence and associativity
b) precedence only
c) associativity only
d) depends on the parser
Answer: a
Explanation: Precedence is a very important factor in determining the order of
evaluation. If two operators have the same precedence, associativity comes into
action.
6\. Find the output of the following prefix expression.
a) 2
b) 12
c) 10
d) 4
Answer: a
Explanation: The given prefix expression is evaluated using two stacks and the
value is given by (2+2-1)\*(4-2)/(5-3+1)= 2.
7\. An error is thrown if the character ‘\n’ is pushed in to the character stack.
a) true
b) false
Answer: b
Explanation: The input character ‘\n’ is accepted as a character by the evaluation
of prefix expression algorithm.
9\. If -\*+abcd = 11, find a, b, c, d using evaluation of prefix algorithm.
a) a=2, b=3, c=5, d=4
b) a=1, b=2, c=5, d=4
c) a=5, b=4, c=7,d=5
d) a=1, b=2, c=3, d=4
Answer: b
Explanation: The given prefix expression is evaluated as ((1+2)\*5)-4 = 11 while
a=1, b=2, c=5, d=4
1\. What is the other name for a postfix expression?
a) Normal polish Notation
b) Reverse polish Notation
c) Warsaw notation
d) Infix notation
Answer: b
3\. Reverse Polish Notation is the reverse of a Polish Notation.
a) True
b) False
Answer: b
Explanation: Reverse Polish Notation is not the reverse of a polish notation.
Though both NPN and RPN read the expression from left to right, they follow
different strategies.
4\. What is the time complexity of evaluation of postfix expression algorithm?
a) O (N)
b) O (N log N)
c) O (N2)
d) O (M log N)
Answer: a
Explanation: The time complexity of evaluation of infix, prefix and postfix
expressions is O (N).
5\. In Postfix expressions, the operators come after the operands.
a) True
b) False
Answer: a
6\. Which of these operators have the highest order of precedence?
a) ‘(‘ and ‘)’
b) ‘\*’ and ‘/’
c) ‘~’ and ‘^’
d) ‘+’ and ‘-‘
Answer: c
Explanation: The highest order of precedence is ~ and ^ followed by ‘\*’ ,’ /’,
‘+’ ,’-‘ and then braces ‘(‘ ‘)’.
7\. Which of the following is not an application of stack?
a) evaluation of postfix expression
b) conversion of infix to postfix expression
c) balancing symbols
d) line at ticket counter
Answer: d
8\. While evaluating a postfix expression, when an operator is encountered, what is
the correct operation to be performed?
a) push it directly on to the stack
b) pop 2 operands, evaluate them and push the result on to the stack
c) pop the entire stack
d) ignore the operator
Answer: b
Explanation: When an operator is encountered, the first two operands are popped
from the stack, they are evaluated and the result is pushed into the stack.
9\. Which of the following statement is incorrect?
a) Postfix operators use value to their right
b) Postfix operators use value to their left
c) Prefix operators use value to their right
d) In postfix expression, operands are followed by operators
Answer: a
Explanation: All prefix operators use values to their right and all postfix
operators use values to their left.
12\. Consider the stack
| 5 |
| 4 |
| 3 |
| 2 |.
At this point, ‘\*’ is encountered. What has to be done?
a) 5\*4=20 is pushed into the stack
b) \* is pushed into the stack
c) 2\*3=6 is pushed into the stack
d) \* is ignored
Answer: a
a=1, b=2, c=3, d=4, e=5, f=6, g=2.
14\. Evaluate and write the result for the following postfix expression
abc\*+de\*f+g\*+ where a=1, b=2, c=3, d=4, e=5, f=6, g=2.
a) 61
b) 59
c) 60
d) 55
Answer: b
Explanation: The infix expression is a+b\*c+(d\*e+f)\*g. Evaluating it, 1+2\*3+(4\
*5+6)\*2 gives 59.
1\. What data structure is used when converting an infix notation to prefix
notation?
a) Stack
b) Queue
c) B-Trees
d) Linked-list
Answer: a
Explanation: First you reverse the given equation and carry out the algorithm of
infix to postfix expression. Here, the data structure used is stacks.
2\. What would be the Prefix notation for the given equation?
A+(B\*C)
a) +A\*CB
b) \*B+AC
c) +A\*BC
d) \*A+CB
Answer: c
5\. Out of the following operators (^, \*, +, &, $), the one having highest
priority is \_\_\_\_\_\_\_\_\_
a) +
b) $
c) ^
d) &
Answer: c
6\. Out of the following operators (|, \*, +, &, $), the one having lowest priority
is \_\_\_\_\_\_\_\_
a) +
b) $
c) |
d) &
Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the
logical OR will have the lowest priority.
7\. What would be the Prefix notation for the given equation?
A^B^C^D
a) ^^^ABCD
b) ^A^B^CD
c) ABCD^^^
d) AB^C^D
Answer: a
8\. What would be the Prefix notation for the given equation?
a+b-c/d\&e|f
a) |&-+ab/cdef
b) &|-+ab/cdef
c) |&-ab+/cdef
d) |&-+/abcdef
Answer: a
9\. What would be the Prefix notation for the given equation?
?
a) -+a\*/^bcdef
b) -+a\*/bc^def
c) -+a\*b/c^def
d) -a+\*/bc^def
Answer: b
11\. What would be the Prefix notation for the given equation?
a|b\&c
a) a|\&bc
b) &|abc
c) |a\&bc
d) ab&|c
Answer: c