250+ TOP MCQs on Stack using Queues and Answers

Data Structure Multiple Choice Questions on “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
Clarification: Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.

2. Making the push operation costly, select the code snippet which implements the same.(let q1 and q2 be two queues)
a)

public void push(int x)
{
        if(empty())
        {
            q1.offer(x);
        }
        else{
                if(q1.size()>0)
                {
                    q2.offer(x);
                    int size = q1.size();
                    while(size>0)
                    {
                        q2.offer(q1.poll());
                        size--;
                    }
                }
            else if(q2.size()>0)
            {
                q1.offer(x);
                int size = q2.size();
                while(size>0)
                {
                    q1.offer(q2.poll());
                    size--;
                }
            }
        }
    }

b)

public void push(int x)
{
        if(empty())
        {
            q1.offer(x);
        }
        else
        {
            if(q1.size()>0)
            {
                q1.offer(x);
                int size = q1.size();
                while(size>0)
                {
                    q2.offer(q1.poll());
                    size--;
                }
            }
            else if(q2.size()>0)
            {
                q2.offer(x);
                int size = q2.size();
                while(size>0)
                {
                    q1.offer(q2.poll());
                    size--;
                }
            }
        }
}

c)

public void push(int x)
{
        if(empty())
        {
            q1.offer(x);
        }
        else
        {
            if(q1.size()>0)
            {
                q2.offer(x);
                int size = q1.size();
                while(size>0)
                {
                    q1.offer(q2.poll());
                    size--;
                }
            }
            else if(q2.size()>0)
            {
                q1.offer(x);
                int size = q2.size();
                while(size>0)
                {
                    q2.offer(q1.poll());
                    size--;
                }
            }
        }
}

d)

public void push(int x)
{
        if(empty())
        {
            q1.offer(x);
        }
        else
        {
            if(q1.size()>0)
            {
                q2.offer(x);
                int size = q1.size();
                while(size>0)
                {
                    q2.offer(q2.poll());
                    size--;
                }
            }
            else if(q2.size()>0)
            {
                q1.offer(x);
                int size = q2.size();
                while(size>0)
                {
                    q2.offer(q1.poll());
                    size--;
                }
            }
        }
}

View Answer

Answer: a
Clarification: Stack follows LIFO principle, hence a new item added must be the first one to exit, but queue follows FIFO principle, so when a new item is entered into the queue, it will be at the rear end of the queue. If the queue is initially empty, then just add the new element, otherwise add the new element to the second queue and dequeue all the elements from the second queue and enqueue it to the first one, in this way, the new element added will be always in front of the queue. Since two queues are needed to realize this push operation, it is considered to be costlier.

 
 
3. Making the push operation costly, select the code snippet which implements the pop operation.
a)

public void pop()
{
        if(q1.size()>0)
        {
            q2.poll();
        }
        else if(q2.size()>0)
        {
            q1.poll();
        }
}

b)

public void pop()
{
        if(q1.size()>0)
        {
            q1.poll();
        }
        else if(q2.size()>0)
        {
            q2.poll();
        }
}

c)

public void pop()
{
        q1.poll();
	q2.poll();
}

d)

public void pop()
{
        if(q2.size()>0)
        {
            q1.poll();
        }
        else
        {
            q2.poll();
        }
}

View Answer

Answer: b
Clarification: As the push operation is costly, it is evident that the required item is in the front of the queue, so just dequeue the element from the queue.

 
 
4. Select the code snippet which returns the top of the stack.
a)

public int top()
{
       if(q1.size()>0)
       {
            return q1.poll();
       }
       else if(q2.size()>0)
       {
            return q2.poll();
       }
       return 0;
}

b)

public int top()
{
       if(q1.size()==0)
       {
            return q1.peek();
       }
       else if(q2.size()==0)
       {
            return q2.peek();
       }
        return 0;
    }

c)

public int top()
{
       if(q1.size()>0)
       {
            return q1.peek();
       }
       else if(q2.size()>0)
       {
            return q2.peek();
       }
       return 0;
}

d)

public int top()
{
       if(q1.size()>0)
       {
            return q2.peek();
       }
       else if(q2.size()>0)
       {
            return q1.peek();
       }
       return 0;
}

View Answer

Answer: c
Clarification: Assuming its a push costly implementation, the top of the stack will be in the front end of the queue, note that peek() just returns the front element, while poll() removes the front element from the queue.

 
 
5. Select the code snippet which return true if the stack is empty, false otherwise.
a)

public boolean empty()
{
     return q2.isEmpty();
}

b)

public boolean empty() 
{
     return q1.isEmpty() || q2.isEmpty();
}

c)

public boolean empty() 
{
     return q1.isEmpty();
}

d)

public boolean empty() 
{
     return q1.isEmpty() & q2.isEmpty();
}

View Answer

Answer: b
Clarification: If both the queues are empty, then the stack also is empty.

 
 
6. Making the pop operation costly, select the code snippet which implements the same.
a)

public int pop()
{
	int res=-999,count=0;
	if(q1.size()>0)
        {
		count = q1.size();
		while(count>0)
			q2.offer(q1.poll());
		res = q1.poll();
	}
	if(q2.size()>0)
        {
		count = q2.size();
		while(count>0)
			q1.offer(q2.poll());
		res = q2.poll();
	}
	return res;
}

b)

public int pop()
{
	int res=-999,count=0;
	if(q1.size()>0)
        {
		count = q1.size();
		while(count>1)
			q2.offer(q1.poll());
		res = q2.poll();
	}
	if(q2.size()>0)
        {
		count = q2.size();
		while(count>1)
			q1.offer(q2.poll());
		res = q1.poll();
	}
	return res;
}

c)

public int pop()
{
	int res=-999,count=0;
	if(q1.size()>0)
        {
		count = q1.size();
		while(count>1)
			q2.offer(q1.poll());
		res = q1.poll();
	}
	if(q2.size()>0)
        {
		count = q2.size();
		while(count>1)
			q1.offer(q2.poll());
		res = q2.poll();
	}
	return res;
}

d)

public int pop()
{
	int res=-999,count=0;
	if(q1.size()>0)
        {
		count = q2.size();
		while(count>1)
			q2.offer(q1.poll());
		res = q1.poll();
	}
	if(q2.size()>0)
        {
		count = q1.size();
		while(count>1)
			q1.offer(q2.poll());
		res = q2.poll();
	}
	return res;
}

View Answer

Answer: c
Clarification: Here the pop operation is costly, hence we need two queues, other than the first element, all the the elements from one queue are dequeued and enqueued to the second queue, hence only one element remains in the first queue which is the item we want, so dequeue it and return the result.

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

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
Clarification: 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.

contest

250+ TOP MCQs on Suffix Array and Answers

Data Structures & Algorithms Multiple Choice Questions on “Suffix Array”.

1. Which of the following is false?
a) Suffix array is always sorted
b) Suffix array is used in string matching problems
c) Suffix array is always unsorted
d) Suffix array contains all the suffixes of the given string

Answer: c
Clarification: Suffix array is always sorted as it contains all the suffixes of a string in sorted order. Suffix arrays are used to solve problems related to string, like string matching problems.

2. Suffix array of the string “statistics” is ____________
a) 2 8 7 4 9 0 5 1 6 3
b) 2 7 4 9 8 0 5 1 6 3
c) 2 4 9 0 5 7 8 1 6 3
d) 2 8 7 0 5 1 6 9 4 3

Answer: a
Clarification: The suffix array of the string statistics will be:
2 atistics
8 cs
7 ics
4 istics
9 s
0 statistics
5 stics
1 tatistics
6 tics
3 tistics
In Suffix array, we only store the indices of suffixes. So, correct option is 2 8 7 4 9 0 5 1 6 3.

3. Suffix array can be created by performing __________ traversal of a suffix tree.
a) breadth-first
b) level order
c) depth-first
d) either breadth-first or level order

Answer: c
Clarification: A suffix tree is a trie, which contains all the suffixes of the given string as their keys and positions in the string as their values. So, we can construct a suffix array by performing the depth-first traversal of a suffix tree.

4. Suffix array is space efficient and faster than the suffix tree.
a) True
b) Fasle

Answer: b
Clarification: Suffix arrays are more space efficient than the suffix trees as they just store the original string and an array of integer. But working with suffix tree is faster than that of the suffix array.

5. If comparison based sorting algorithm is used construct the suffix array, then what will be time required to construct the suffix array?
a) O(nlogn)
b) O(n2)
c) O(n2logn)
d) O(n2) + O(logn)

Answer: c
Clarification: On average comparison based sorting algorithms require O(nlogn) comparisons. But comparing a suffix takes O(n). So, overall time to construct the suffix array will be O(nlogn) * O(n) = O(n2logn).

6. What will be the suffix array of the string “engineering”?
a) 2 3 8 4 9 1 7 5 0 6 10
b) 5 0 6 1 4 9 1 7 0 2 3 8
c) 5 0 6 10 2 4 9 1 7 3 8
d) 5 0 6 10 2 3 8 4 9 1 7

Answer: d
Clarification: Correct choice is : 5 0 6 10 2 3 8 4 9 1 7.
Because the suffix array formed will be: 5 eering 0 engineering 6 ering 10 g 2 gineering 3 ineering 8 ing 4 neering 9 ng 1 ngineering 7 ring.

7. LCP array and ______ is used to construct suffix tree.
a) Hash tree
b) Hash trie
c) Suffix array
d) Balanced tree

Answer: c
Clarification: Suffix tree can be created using an LCP array and a suffix array. If we are given a string of length (n + 1) and its suffix array and LCP array, we can construct the suffix tree in linear time i.e in O(n) time.

8. What is the time required to locate the occurrences of a pattern P of length m in a string of length n using suffix array?
a) O(nm)
b) O(n2)
c) O(mnlogn)
d) O(mlogn)

Answer: d
Clarification: Suffix arrays are used to find the occurrences of a pattern in a string. Pattern of length m will require m characters to compare, so using suffix array we can find occurrences of a pattern in the string of length n in O(mlogn) time.

9. Suffix array can be created in O(nlogn) time.
a) True
b) False

Answer: a
Clarification: Suffix array can be constructed in O(n2logn) time using sorting algorithms but it is possible to build the suffix array in O(nlogn) time using prefix doubling.

10. Which of the following is/are advantages suffix array one suffix tree?
I. Lesser space requirement
II. Improved cache locality
III. Easy construction in linear time
a) Only I
b) All I, II and III
c) Only I and III
d) Only II and III

Answer: b
Clarification: Advantages of the suffix array over suffix tree are : (i) Lesser space requirement (ii) Improved cache locality and (iii) Simple algorithms to construct suffix arrays in linear time.

250+ TOP MCQs on Inorder Traversal and Answers

Data Structure Multiple Choice Questions on “Inorder Traversal”.

1. For the tree below, write the in-order traversal.
data-structure-questions-answers-inorder-traversal-q1
a) 6, 2, 5, 7, 11, 2, 5, 9, 4
b) 6, 5, 2, 11, 7, 4, 9, 5, 2
c) 2, 7, 2, 6, 5, 11, 5, 9, 4
d) 2, 7, 6, 5, 11, 2, 9, 5, 4
Answer: a
Clarification: In-order traversal follows LNR(Left-Node-Right).

2. For the tree below, write the level-order traversal.
data-structure-questions-answers-inorder-traversal-q1
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 11, 9, 6, 5, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
Answer: b
Clarification: Level order traversal follows a breadth first search approach.

3. Select the code snippet which performs in-order traversal.
a)

public void inorder(Tree root)
{
	System.out.println(root.data);
	inorder(root.left);
	inorder(root.right);
}

b)

public void inorder(Tree root)
{
	inorder(root.left);
	System.out.println(root.data);
	inorder(root.right);
}

c)

public void inorder(Tree root)
{
	System.out.println(root.data);
	inorder(root.right);
	inorder(root.left);
}

d)

public void inorder(Tree root)
{
	inorder(root.right);
	inorder(root.left);
	System.out.println(root.data);
}

View Answer

Answer: b
Clarification: In-order traversal follows LNR(Left-Node-Right).

 
 

4. Select the code snippet which performs level-order traversal.
a)

public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.left!=null)  
        queue.add(tempNode.left);  
        if(tempNode.right!=null)  
        queue.add(tempNode.right);  
    }  
}

b)

public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.left!=null)  
        queue.add(tempNode.right);  
        if(tempNode.right!=null)  
        queue.add(tempNode.left);  
    }  
}

c)

public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.right!=null)  
        queue.add(tempNode.left);  
        if(tempNode.left!=null)  
        queue.add(tempNode.right);  
    }  
}

d)

public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.right!=null)  
        queue.add(tempNode.left.left);  
        if(tempNode.left!=null)  
        queue.add(tempNode.right.right);  
    }  
}

View Answer

Answer: a
Clarification: Firstly add the root node to the queue. Then for all the remaining nodes, pop the front end of the queue and print it, add the left and right children of the popped node to the queue.

 
 

5. What is the space complexity of the in-order traversal in the recursive fashion? (d is the tree depth and n is the number of nodes)
a) O(1)
b) O(nlogd)
c) O(logd)
d) O(d)
Answer: d
Clarification: In the worst case we have d stack frames in the recursive call, hence the complexity is O(d).

6. What is the time complexity of level order traversal?
a) O(1)
b) O(n)
c) O(logn)
d) O(nlogn)
Answer: b
Clarification: Since you have to go through all the nodes, the complexity becomes O(n).

7. Which of the following graph traversals closely imitates level order traversal of a binary tree?
a) Depth First Search
b) Breadth First Search
c) Depth & Breadth First Search
d) Binary Search
Answer: b
Clarification: Both level order tree traversal and breadth first graph traversal follow the principle that visit your neighbors first and then move on to further nodes.

8. In a binary search tree, which of the following traversals would print the numbers in the ascending order?
a) Level-order traversal
b) Pre-order traversal
c) Post-order traversal
d) In-order traversal
Answer: d
Clarification: In a binary search tree, a node’s left child is always lesser than the node and right child is greater than the node, hence an in-order traversal would print them in a non decreasing fashion.

250+ TOP MCQs on Rope and Answers

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

1. Which of the following is also known as Rope data structure?
a) Cord
b) String
c) Array
d) Linked List

Answer: a
Clarification: 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.

2. Which type of data structure does rope represent?
a) Array
b) Linked List
c) Queue
d) Binary Tree

Answer: d
Clarification: Rope is a special binary tree in which the end nodes contain the string and its length. The array is a linear data structure. Linked List is a linear data structure having a node containing data input and the address of the next node. The queue is a data structure working on the principle of FIFO.

3. What is the time complexity for finding the node at x position where n is the length of the rope?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)

Answer: a
Clarification: In order to find the node at x position in a rope data structure where N is the length of the rope, we start a recursive search from the root node. So the time complexity for worst case is found to be O (log N).

4. What is the time complexity for creating a new node and then performing concatenation in the rope data structure?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)

Answer: d
Clarification: In order to perform the concatenation on the rope data structure, one can create two nodes S1 and S2 and then performing the operation in constant time that is the time complexity is O (1).

5. What is the time complexity for splitting the string into two new string in the rope data structure?
a) O (n2)
b) O (n!)
c) O (log n)
d) O (1)

Answer: c
Clarification: In order to perform the splitting on the rope data structure, one can split the given string into two new string S1 and S2 in O (log n) time. So, the time complexity for worst case is O (log n).

6. Which type of binary tree does rope require to perform basic operations?
a) Unbalanced
b) Balanced
c) Complete
d) Full

Answer: b
Clarification: To perform the basic operations on a rope data structure like insertion, deletion, concatenation and splitting, the rope should be a balanced tree. After performing the operations one should again re-balance the tree.

7. What is the time complexity for inserting the string and forming a new string in the rope data structure?
a) O (log n)
b) O (n!)
c) O (n2)
d) O (1)

Answer: a
Clarification: In order to perform the insertion on the rope data structure, one can insert the given string at any position x to form a new string in O (log n) time. So, the time complexity for worst case is O (log n). This can be done by one split operation and two concatenation operations.

8. Is insertion and deletion operation faster in rope than an array?
a) True
b) False

Answer: a
Clarification: In order to perform the insertion on the rope data structure, the time complexity is O (log n). In order to perform the deletion on the rope data structure, the time complexity for worst case is O (log n). While for arrays the time complexity is O (n).

9. What is the time complexity for deleting the string to form a new string in the rope data structure?
a) O (n2)
b) O (n!)
c) O (log n)
d) O (1)

Answer: c
Clarification: In order to perform the deletion on the rope data structure, one can delete the given string at any position x to form a new string in O (log n) time. So, the time complexity for worst case is O (log n). This can be done by two split operations and one concatenation operation.

10. Is it possible to perform a split operation on a string in the rope if the split point is in the middle of the string.
a) True
b) False

Answer: a
Clarification: In order to perform the splitting on the rope data structure, one can split the given string into two new string S1 and S2 in O (log n) time. So, the time complexity for worst case is O (log n). The split operation can be performed if the split point is either at the end of the string or in the middle of the string.

250+ TOP MCQs on Binomial and Fibonacci Heap and Answers

Data Structure Multiple Choice Questions on “Binomial and Fibonacci Heap”.

1. The main distinguishable characterstic of a binomial heap from a binary heap is that
a) it allows union operations very efficiently
b) it does not allow union operations that could easily be implemented in binary heap
c) the heap structure is not similar to complete binary tree
d) the location of child node is not fixed i.e child nodes could be at level (h-2) or (h-3), where h is height of heap and h>4
Answer: a
Clarification: The main use of binomial heap is to unify two different heap efficiently.

2. The number of trees in a binomial heap with n nodes is
a) logn
b) n
c) nlogn
d) n/2
Answer: a
Clarification: At each depth there is a binomial tree in a binomial heap.

3. In a binomial heap the root value is greater than left child and less than right child.
a) True
b) False
Answer: b
Clarification: Binomial tree used in making binomial heap follows min heap property.

4. Given the pseudo code, state whether the function for merging of two heap is correct or not?

		mergeTree(p,q)
    		if p.root.value <= q.root.value
        	return p.addTree(q)
    		else
        	return q.addTree(p)

a) True
b) False
Answer: a
Clarification: Binomial heap has a property that root value is less than both the child node’s value. So the given function of merging two different heap is correct.

5. What is order of resultant heap after merging two tree of order k?
a) 2*k
b) k+1
c) k*k
d) k+logk
Answer: b
Clarification: This could be easily verified by looking at the structure of a binomial heap.

6. Time taken in decreasing the node value in a binomial heap is
a) O(n)
b) O(1)
c) O(logn)
d) O(nlogn)
Answer: c
Clarification: Decreasing a node value may result in violating the min property. As a result be there would be exchange in the value of parent and child which at max goes up to height of the heap.

7. What does this pseudo_code return ?

	int myfun(heap_arr[])
	{
		int mini=INF;
		for(int i=0;i<tot_node;i++)
		mini=min(mini,heap_arr)
		return mini;
	}

a) Last added element to heap
b) First element added to heap
c) Root of the heap
d) Leftmost node of the heap
Answer: c
Clarification: The function return minimum value in the heap_Array which is equal to the root value of the heap.

8. Which of these operations have same complexities?
a) Insertion, find_min
b) Find_min, union
c) Union, Insertion
d) Deletion, Find _max
Answer: c
Clarification: With proper implementation using link list find_min and find_max operation can be done in O(1), while the remaining takes O(logn) time.

9. The Statement “Fibonacci heap has better amortized running time in compare to a binomial heap”.
a) True
b) False
Answer: a
Clarification: Overall complexity of insertion, merging, deleting is in order of O((a+b)logn) For Fibonacci the complexity reduces to O(a+ blogn).

10. Given a heap of n nodes.The maximum number of tree for building the heap is.
a) n
b) n-1
c) n/2
d) logn
Answer: a
Clarification: Each node could be seen as a tree with only one node and as a result maximum subtree in the heap is equal to number of nodes in the heap.

11. Choose the option with function having same complexity for a fibonacci heap.
a) Insertion, Union
b) Insertion, Deletion
c) extract_min, insertion
d) Union, delete
Answer: a
Clarification: For a fibonacci heap insertion, union take O(1) while remaining take O(logn) time.

12. What is wrong with the following code of insertion in fibonacci heap.
Choose the correct option

	FIB-INSERT(H, x)
	degree[x]= 0
	p[x]=  NIL
	child[x] =NIL
	left[x] =x
	right[x] =x
	mark[x] =FALSE
	concatenate the root list containing x with root list H 
	if min[H] = NIL or key[x] > key[min[H]]
	then min[H]= x
	n[H]= n[H] + 1

a) Line -11
b) Line -3
c) Line 9
d) Line 7
Answer: c
Clarification: The main characterstics of a fibonacci heap is violated since min[H] must conatin one with smallest value.

13. What will be the order of new heap created after union of heap H1 and H2 when created by the following code.Initially both are of the order n.

	FIB_UNION(H1,H2)
	{
		H =MAKE_HEAP()
		min[H]= min[H1]
		concatenate the root list of H2 with the root list of H
		if (min[H1] = NIL) or (min[H2]!= NIL and min[H2] < min[H1])
		then min[H] = min[H2]
		n[H]=  n[H1] + n[H2]
		free the objects H1 and H2
		return H
	}

a) n+1
b) n+n/2
c) nlogn
d) 2*n
Answer: a
Clarification: Union of two trees increase the order of the resultant by atmost value 1.

250+ TOP MCQs on Hash Tables with Linear Probing and Answers

Data Structures & Algorithms Multiple Choice Questions on “Hash Tables with Linear Probing”.

1. Which of the following problems occur due to linear probing?
a) Primary collision
b) Secondary collision
c) Separate chaining
d) Extendible hashing

Answer: a
Clarification: Primary collision occurs due to linear probing technique. It is overcome using a quadratic probing technique.

2. How many probes are required on average for insertion and successful search?
a) 4 and 10
b) 2 and 6
c) 2.5 and 1.5
d) 3.5 and 1.5

Answer: c
Clarification: Using formula, the average number of probes required for insertion is 2.5 and for a successful search, it is 1.5.

3. What is the load factor for an open addressing technique?
a) 1
b) 0.5
c) 1.5
d) 0

Answer: b
Clarification: The load factor for an open addressing technique should be 0.5. For separate chaining technique, the load factor is 1.

4. Which of the following is not a collision resolution strategy for open addressing?
a) Linear probing
b) Quadratic probing
c) Double hashing
d) Rehashing

Answer: d
Clarification: Linear probing, quadratic probing and double hashing are all collision resolution strategies for open addressing whereas rehashing is a different technique.

5. In linear probing, the cost of an unsuccessful search can be used to compute the average cost of a successful search.
a) True
b) False

Answer: a
Clarification: Using random collision resolution algorithm, the cost of an unsuccessful search can be used to compute the average cost of a successful search.

6. Which of the following is the correct function definition for linear probing?
a) F(i)= 1
b) F(i)=i
c) F(i)=i2
d) F(i)=i+1

Answer: b
Clarification: The function used in linear probing is defined as, F(i)=I where i=0,1,2,3….,n.

7. ___________ is not a theoretical problem but actually occurs in real implementations of probing.
a) Hashing
b) Clustering
c) Rehashing
d) Collision

Answer: b
Clarification: Clustering is not a theoretical problem but it occurs in implementations of hashing. Rehashing is a kind of hashing.

8. What is the hash function used in linear probing?
a) H(x)= key mod table size
b) H(x)= (key+ F(i2)) mod table size
c) H(x)= (key+ F(i)) mod table size
d) H(x)= X mod 17

Answer: c
Clarification: The hash function used in linear probing is defined to be H(x)= (key+ F(i)) mod table size where i=0,1,2,3,…,n.

9. Hashing can be used in online spelling checkers.
a) True
b) False

Answer: a
Clarification: If misspelling detection is important, an entire dictionary can be pre-hashed and words can be checked in constant time.

10. In the following given hash table, use linear probing to find the location of 49.

0
1
2
3
4
5
6
7
8 18
9 89

a) 7
b) 6
c) 2
d) 0

Answer: d
Clarification: Initially, collision occurs while hashing 49 for the first time.
Hence, after setting f(i)=1, the hashed location is found to be 0.

11. What is the formula to find the expected number of probes for an unsuccessful search in linear probing?
a) (frac{1}{2} frac{1+1}{(1-⅄)})
b) (frac{1}{2}frac{1+1}{(1-⅄)^2})
c) (frac{1}{2}frac{1+1}{(1+⅄)})
d) (frac{1}{2}frac{1+1}{(1+⅄)(1-⅄)})

Answer: b
Clarification: The mathematical formula for calculating the number of probes for an unsuccessful search is (frac{1}{2}frac{1+1}{(1-⅄)^2}). For insertion, it is (frac{1}{2} frac{1+1}{(1-⅄)}).