250+ TOP MCQs on Hash Tables Chaining using Linked Lists and Answers

Data Structures & Algorithms Multiple Choice Questions on “Hash Tables Chaining using Linked Lists”.

1. The case in which a key other than the desired one is kept at the identified location is called?
a) Hashing
b) Collision
c) Chaining
d) Open addressing

Answer: b
Clarification: When some other value is placed at a specified location other than the desired key, it is said to be a collision.

2. What data organization method is used in hash tables?
a) Stack
b) Array
c) Linked list
d) Queue

Answer: c
Clarification: The data structure used to organize data for hash tables is linked list. It contains a data field and a pointer field.

3. The task of generating alternative indices for a node is called?
a) Collision handling
b) Collision detection
c) Collision recovery
d) Closed hashing

Answer: a
Clarification: Collision handling involves the process of formulating alternative indices for a key.

4. Which of the following is not a collision resolution technique?
a) Separate chaining
b) Linear probing
c) Quadratic probing
d) Hashing

Answer: d
Clarification: Hashing is a technique of placing data items in specific locations. Collision may occur in hashing but hashing is not a collision resolution technique.

5. Hashing is the problem of finding an appropriate mapping of keys into addresses.
a) True
b) False

Answer: a
Clarification: Hashing is a data structure which is used to locate data in a table based on a key value.

6. In a hash table of size 10, where is element 7 placed?
a) 6
b) 7
c) 17
d) 16

Answer: b
Clarification: The hash location is defined as hash(f)= key mod table_size.
7 mod 10 gives 7. It is placed in 7th position.

7. What should be the load factor for separate chaining hashing?
a) 0.5
b) 1
c) 1.5
d) 2

Answer: b
Clarification: For hashing using separate chaining method, the load factor should be maintained as 1. For open addressing method, it should not exceed 0.5.

8. Which of the following operations are done in a hash table?
a) Insert only
b) Search only
c) Insert and search
d) Replace

Answer: c
Clarification: Hash tables are used to implement Insert and Find operations in constant average time. This is the prime purpose of hashing.

9. Which of the following is identical to that of a separate chaining hash node?
a) Linked list
b) Array
c) Stack
d) Queue

Answer: a
Clarification: The hash node in separate chaining is similar to that of a linked list. The separate chaining hash table is an array of linked lists.

10. Which of the following is the hashing function for separate chaining?
a) H(x)=(hash(x)+f(i)) mod table size
b) H(x)=hash(x)+i2 mod table size
c) H(x)=x mod table size
d) H(x)=x mod (table size * 2)

Answer: c
Clarification: The hashing function for separate chaining is given by H(x)= key mod table size. H(x)=hash(x)+i2 mod table size defines quadratic probing.

11. What is the correct notation for a load factor?
a) Ω
b) ∞
c) ∑
d) ⅄

Answer: d
Clarification: In general, load factor is denoted as ⅄. In separate chaining method, load factor is maintained as 1.0.

12. In hash tables, how many traversal of links does a successful search require?
a) 1+⅄
b) 1+⅄2
c) 1+ (⅄/2)
d) ⅄3

Answer: c
Clarification: A successful search requires about 1+ (⅄/2) links to be traversed. There is a guarantee that at least one link must be traversed.

13. Which of the following is a disadvantage of using separate chaining using linked lists?
a) It requires many pointers
b) It requires linked lists
c) It uses array
d) It does not resolve collision

Answer: a
Clarification: One of the major disadvantages of using separate chaining is the requirement of pointers. If the number of elements are more, it requires more pointers.

14. What is the worst case search time of a hashing using separate chaining algorithm?
a) O(N log N)
b) O(N)
c) O(N2)
d) O(N3)

Answer: b
Clarification: The worst case search time of separate chaining algorithm using linked lists is mathematically found to be O(N).

250+ TOP MCQs on Directed Graph and Answers

Data Structure Multiple Choice Questions on “Directed Graph”.

1. Dijkstra’s Algorithm will work for both negative and positive weights?
a) True
b) False
Answer: b
Clarification: Dijkstra’s Algorithm assumes all weights to be non-negative.

2. A graph having an edge from each vertex to every other vertex is called a ___________
a) Tightly Connected
b) Strongly Connected
c) Weakly Connected
d) Loosely Connected
Answer: a
Clarification: This is a part of the nomenclature followed in Graph Theory.

3. What is the number of unlabeled simple directed graph that can be made with 1 or 2 vertices?
a) 2
b) 4
c) 5
d) 9
Answer: b
Clarification:data-structure-questions-answers-directed-graph-q3

4. Floyd Warshall Algorithm used to solve the shortest path problem has a time complexity of __________
a) O(V*V)
b) O(V*V*V)
c) O(E*V)
d) O(E*E)
Answer: b
Clarification: The Algorithm uses Dynamic Programming and checks for every possible path.

5. All Graphs have unique representation on paper.
a) True
b) False
Answer: b
Clarification: Same Graph may be drawn in different ways on paper.

6. Assuming value of every weight to be greater than 10, in which of the following cases the shortest path of a directed weighted graph from 2 vertices u and v will never change?
a) add all values by 10
b) subtract 10 from all the values
c) multiply all values by 10
d) in both the cases of multiplying and adding by 10
Answer: c
Clarification: In case of addition or subtraction the shortest path may change because the number of edges between different paths may be different, while in case of multiplication path wont change.

7. What is the maximum possible number of edges in a directed graph with no self loops having 8 vertices?
a) 28
b) 64
c) 256
d) 56
Answer: d
Clarification: If a graph has V vertices than every vertex can be connected to a possible of V-1 vertices.

8. What would be the DFS traversal of the given Graph?
data-structure-questions-answers-directed-graph-q8
a) ABCED
b) AEDCB
c) EDCBA
d) ADECB
Answer: a
Clarification: In this case two answers are possible including ADEBC.

9. What would be the value of the distance matrix, after the execution of the given code?

#include 
#define INF 1000000
int graph[V][V] = {   {0,   7,  INF, 4},
                      {INF, 0,   13, INF},
                      {INF, INF, 0,   12},
                      {INF, INF, INF, 0}
                  };
 
int distance[V][V], i, j, k;
 
for (i = 0; i < V; i++)
        for (j = 0; j < V; j++)
    	distance[i][j] = graph[i][j];
 
for (k = 0; k < V; k++)
	for (i = 0; i < V; i++)
        	for (j = 0; j < V; j++)
                {
            		if (distance[i][k] + distance[k][j] < distance[i][j])
                		distance[i][j] = distance[i][k] + distance[k][j];
 
                           return 0;
                }

a)

{            
    {0,   7,  INF, 4},
    {INF, 0,   13, INF},
    {INF, INF, 0,   12},
    {INF, INF, INF, 0}
};

b)

{            
    {0,   7,  20, 24},
    {INF, 0,   13, 25},
    {INF, INF, 0,   12},
    {INF, INF, INF, 0}
};

c)

{ 
    {0,   INF,  20, 24},
    {INF, INF,   13, 25},
    {INF, INF, 0,   12},
    {INF, INF, INF, 0}
    {INF, 0,   13, 25},
    {INF, INF, 0,   12},
    {24, INF, INF, 0}
};

d) None of the mentioned
Answer: b
Clarification: The program computes the shortest sub distances.

10. What is the maximum number of edges present in a simple directed graph with 7 vertices if there exists no cycles in the graph?
a) 21
b) 7
c) 6
d) 49
Answer: c
Clarification: If the no cycles exists then the difference between the number of vertices and edges is 1.

250+ TOP MCQs on Priority Queue and Answers

Data Structure Multiple Choice Questions on “Priority Queue”.

1. With what data structure can a priority queue be implemented?
a) Array
b) List
c) Heap
d) Tree
Answer: d
Clarification: 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
Clarification: Undo operation is achieved using a stack.

3. Select the appropriate code that inserts elements into the list based on the given key value.
(head and trail are dummy nodes to mark the end and beginning of the list, they do not contain any priority or element)
a)

public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = head;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(temp);
			temp.setNext(dup);
		}
		count++;
	}
}

b)

public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = dup;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(temp);
			temp.setNext(dup);
		}
		count++;
	}
}

c)

public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = head;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = dup.getNext();
				cur = cur.getNext();
			}
			cur.setNext(dup);
			temp.setNext(cur);
		}
		count++;
	}
}

d)

public void insert_key(int key,Object item) 
{
	if(key<0)
	{
		Systerm.our.println("invalid");
		System.exit(0);
	}
	else
	{
		Node temp = new Node(key,item,null);
		if(count == 0)
		{
			head.setNext(temp);
			temp.setNext(trail);
		}
		else
		{
			Node dup = head.getNext();
			Node cur = head;
			while((key>dup.getKey()) && (dup!=trail))
			{
				dup = cur
				cur = cur.getNext();
			}
			cur.setNext(dup);
			temp.setNext(cur);
		}
		count++;
	}
}

View Answer

Answer: a
Clarification: Have two temporary pointers ‘dup’ and ‘cur’ with ‘cur’ trailing behind ‘dup’. Traverse through the list until the given key is greater than some element with a lesser key, insert the new node ‘temp’ in that position.

 
 

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
Clarification: In the worst case, you might have to traverse the entire list.

5. What is the functionality of the following piece of code?

public Object delete_key() 
{
	if(count == 0)
	{
		System.out.println("Q is empty");
		System.exit(0);
	}
	else
	{
		Node cur = head.getNext();
		Node dup = cur.getNext();
		Object e = cur.getEle();
		head.setNext(dup);
		count--;
		return e;
	}
}

a) Delete the second element in the list
b) Return but not delete the second element in the list
c) Delete the first element in the list
d) Return but not delete the first element in the list
Answer: c
Clarification: A pointer is made to point at the first element in the list and one more to point to the second element, pointer manipulations are done such that the first element is no longer being pointed by any other pointer, its value is returned.

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
Clarification: 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
Answer: d
Clarification: 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
Clarification: In the worst case, you might have to traverse the entire list.

250+ TOP MCQs on Dynamic Array and Answers

Data Structure Multiple Choice Questions on “Dynamic Array”.

1. What is a dynamic array?
a) A variable size data structure
b) An array which is created at runtime
c) The memory to the array is allocated at runtime
d) An array which is reallocated everytime whenever new elements have to be added
Answer: a
Clarification: It is a varying-size list data structure that allows items to be added or removed, it may use a fixed sized array at the back end.

2. What is meant by physical size in a dynamic array?
a) The size allocated to elements
b) The size extended to add new elements
c) The size of the underlying array at the back-end
d) The size visible to users
Answer: c
Clarification: Physical size, also called array capacity is the size of the underlying array, which is the maximum size without relocation of data.

3. The number of items used by the dynamic array contents is its __________
a) Physical size
b) Capacity
c) Logical size
d) Random size
Answer: c
Clarification: The number of items used by the dynamic array contents is called logical size. Physical size is the size of the underlying array, which is the maximum size without reallocation of data.

4. How will you implement dynamic arrays in Java?
a) Set
b) Map
c) HashMap
d) List
Answer: d
Clarification: ArrayList is used to implement dynamic arrays in Java.

5. Which of the following is the correct syntax to declare an ArrayList in Java?
a) ArrayList al = new ArrayList();
b) ArrayList al = new ArrayList[];
c) ArrayList al() = new ArrayList();
d) ArrayList al[] = new ArrayList[];
Answer: a
Clarification: This is a non-generic way of creating an ArrayList.

6. Array is divided into two parts in ____________
a) Hashed Array Tree
b) Geometric Array
c) Bounded-size dynamic array
d) Sparse Array
Answer: c
Clarification: The first part stores the items of the dynamic array and the second part is reserved for new allocations.

7. Which of the following is a disadvantage of dynamic arrays?
a) Locality of reference
b) Data cache utilization
c) Random access
d) Memory leak
Answer: d
Clarification: Dynamic arrays share the advantage of arrays, added to it is the dynamic addition of elements to the array. Memory can be leaked if it is not handled properly during allocation and deallocation. It is a disadvantage.

8. What is the time complexity for inserting/deleting at the beginning of the array?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)
Answer: b
Clarification: All the other elements will have to be moved, hence O(n).

9. Dynamic arrays overcome the limit of static arrays.
a) True
b) False
Answer: a
Clarification: Static arrays have fixed capacity. The capacity must be specified during memory allocation. Dynamic arrays don’t require to specify their capacity during memory allocation. Dynamic arrays have fixed physical size at backend and its capacity increases if required. Thus, Dynamic arrays overcome the limit of static arrays.

10. The size of the dynamic array is deallocated if the array size is less than _________% of the backend physical size.
a) 30
b) 40
c) 10
d) 20
Answer: a
Clarification: The size of the dynamic array is decreased/deallocated if the actual size of the array is less than 30% of the backend physical size. This is used to avoid memory wastage.

11. Both Dynamic array and Dynamically memory allocated array are same.
a) True
b) False
Answer: b
Clarification: Physical size of a Dynamic array is fixed with a larger value. Dynamically memory allocated arrays are arrays whose memory is allocated at run time rather than at compile time. Dynamically memory allocated arrays don’t have physical size at the backend. Thus, Dynamic arrays and Dynamically memory allocated arrays are different.

12. In which of the following cases dynamic arrays are not preferred?
a) If the size of the array is unknown
b) If the size of the array changes after few iterations
c) If the memory reallocation takes more time i.e. expensive
d) If the array holds less number of elements
Answer: d
Clarification: Dynamic arrays are preferred when the size of the array is unknown during memory allocation or the size changes after few iterations or the memory reallocation is expensive. If array holds less number of elements, the physical size is reduced and reduction takes more time. In that case, we can use normal arrays instead of dynamic arrays.

13. The growth factor of ArrayList in Java is _______
a) 1
b) 1.5
c) 2
d) 0
Answer: b
Clarification: The growth factor of dynamic arrays (Array List) in Java is 3/2.
The new array capacity is calculated as new_array_size = (old_array_size*3)/2+1.

14. In special case, the time complexity of inserting/deleting elements at the end of dynamic array is __________
a) O (n)
b) O (n1/2)
c) O (log n)
d) O (1)
Answer: a
Clarification: In general, the time complexity of inserting or deleting elements at the end of dynamic array is O (1). Elements are added at reserved space of dynamic array. If this reserved space is exceeded, then the physical size of the dynamic array is reallocated and every element is copied from original array. This will take O(n) time to add new element at the end of the array.

15. Which of the following arrays are used in the implementation of list data type in python?
a) Bit array
b) Dynamic arrays
c) Sparse arrays
d) Parallel arrays
Answer: b
Clarification: Dynamic arrays are used in the implementation of list data type in python. Sparse arrays are used in the implementation of sparse matrix in Numpy module. All bit array operations are implemented in bitarray module.

250+ TOP MCQs on Binary Tree Operations and Answers

Data Structures & Algorithms Multiple Choice Questions on “Binary Tree Operations”.

1. What is the maximum number of children that a binary tree node can have?
a) 0
b) 1
c) 2
d) 3

Answer: c
Clarification: In a binary tree, a node can have atmost 2 nodes (i.e.) 0,1 or 2 left and right child.

2. Which of the following properties are obeyed by all three tree – traversals?
a) Left subtrees are visited before right subtrees
b) Right subtrees are visited before left subtrees
c) Root node is visited before left subtree
d) Root node is visited before right subtree

Answer: a
Clarification: In preorder, inorder and postorder traversal the left subtrees are visited before the right subtrees. In Inorder traversal, the Left subtree is visited first then the Root node then the Right subtree. In postorder traversal, the Left subtree is visited first, then Right subtree and then the Root node is visited.

3. A binary tree is a rooted tree but not an ordered tree.
a) true
b) false

Answer: b
Clarification: A binary tree is a rooted tree and also an ordered tree (i.e) every node in a binary tree has at most two children.

4. How many common operations are performed in a binary tree?
a) 1
b) 2
c) 3
d) 4

Answer: c
Clarification: Three common operations are performed in a binary tree- they are insertion, deletion and traversal.

5. What is the traversal strategy used in the binary tree?
a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) Priority traversal

Answer: b
Clarification: Breadth first traversal, also known as level order traversal is the traversal strategy used in a binary tree. It involves visiting all the nodes at a given level.

6. How many types of insertion are performed in a binary tree?
a) 1
b) 2
c) 3
d) 4

Answer: b
Clarification: Two kinds of insertion operation is performed in a binary tree- inserting a leaf node and inserting an internal node.

7. Using what formula can a parent node be located in an array?
a) (i+1)/2
b) (i-1)/2
c) i/2
d) 2i/2

Answer: b
Clarification: If a binary tree is represented in an array, parent nodes are found at indices (i-1)/2.

8. General ordered tree can be encoded into binary trees.
a) true
b) false

Answer: a
Clarification: General ordered tree can be mapped into binary tree by representing them in a left-child-right-sibling way.

9. How many bits would a succinct binary tree occupy?
a) n+O(n)
b) 2n+O(n)
c) n/2
d) n

Answer: b
Clarification: A succinct binary tree occupies close to minimum possible space established by lower bounds. A succinct binary tree would occupy 2n+O(n) bits.

10. The average depth of a binary tree is given as?
a) O(N)
b) O(√N)
c) O(N2)
d) O(log N)

Answer: d
Clarification: The average depth of a binary tree is given as O(√N). In case of a binary search tree, it is O(log N).

11. How many orders of traversal are applicable to a binary tree (In General)?
a) 1
b) 4
c) 2
d) 3

Answer: d
Clarification: The three orders of traversal that can be applied to a binary tree are in-order, pre-order and post order traversal.

12. If binary trees are represented in arrays, what formula can be used to locate a left child, if the node has an index i?
a) 2i+1
b) 2i+2
c) 2i
d) 4i

Answer: a
Clarification: If binary trees are represented in arrays, left children are located at indices 2i+1 and right children at 2i+2.

250+ TOP MCQs on Treap and Answers

Data Structures & Algorithms Multiple Choice Questions on “Treap”.

1. What is the space complexity of a treap algorithm?
a) O(N)
b) O(log N)
c) O(log N)
d) O(N2)

Answer: a
Clarification: The average case and worst case space complexity of a treap is mathematically found to be O(N).

2. A treap is a combination of a tree and a heap.
a) false
b) true

Answer: b
Clarification: A treap is a combination of a tree and a heap. The structure of a treap is determined by the fact that it is heap-ordered.

3. Which is the simplest of all binary search trees?
a) AVL tree
b) Treap
c) Splay tree
d) Binary heap

Answer: b
Clarification: A treap is the simplest of all binary search trees. Each node is given a numeric priority and implementation is non recursive.

4. What is the reason behind the simplicity of a treap?
a) Each node has data and a pointer
b) Each node is colored accordingly
c) It is a binary search tree following heap principles
d) Each node has a fixed priority field

Answer: d
Clarification: A treap is the simplest of all because we don’t have to worry about adjusting the priority of a node.

5. What is the condition for priority of a node in a treap?
a) a node’s priority should be greater than its parent
b) a node’s priority should be at least as large as its parent
c) the priority is randomly assigned and can have any value
d) a node’s priority is always given in decreasing order

Answer: b
Clarification: A node’s priority should satisfy heap order. That is, any node’s priority should be at least as large as its parent.

6. Several other operations like union set difference and intersection can be done in treaps.
a) True
b) False

Answer: a
Clarification: Other than insertion, deletion and search operations, several operations like union, intersection and set difference can be done in treaps.

7. What is the average running time of a treap?
a) O(N)
b) O(N log N)
c) O(log N)
d) O(M log N)

Answer: c
Clarification: The average case and worst case analysis of a treap are mathematically found to be O(log N).

8. Which node has the lowest priority in a treap?
a) root node
b) leaf node
c) null node
d) centre node

Answer: a
Clarification: A root node has the lowest priority in a treap since the node’s priority is based on heap order.

9. What is the priority of a null node?
a) 1
b) 0
c) random number
d) infinity

Answer: d
Clarification: The priority of a null node is set to be infinity in a treap so that during deletion, priority of that particular node is set to infinity, rotated and freed.

10. Who invented treaps?
a) Cecilia and Raimund
b) Arne Andersson
c) Donald Shell
d) Harris and Ross

Answer: a
Clarification: Cecilia and Raimund invented Treaps. Arne Andersson invented AA – Trees. Donald Shell invented shell sort and Harris and Ross formulated maximum flow problem.