Top MCQs on Binary Search Algorithm with Answers

Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N).
More on Binary Search

Binary Search Quiz

Binary Search Quiz


Question 1

What is the worst case time complexity of insertion sort where position of the data to be inserted is calculated using binary search?

Cross

N

Cross

N*log(N)

Tick

N2

Cross

N*log(N)2



Question 1-Explanation: 

Applying binary search to calculate the position of the data to be inserted doesn't reduce the time complexity of insertion sort.

This is because the insertion of data at an appropriate position involves two steps:

  1. Calculate the position.
  2. Shift the data from the position calculated in step #1 one step right to create a gap where the data will be inserted.
  3. Using binary search reduces the time complexity in step #1 from O(N) to O(log(N)). But, the time complexity in step #2 still remains O(N).

 So, overall complexity remains O(N2).

Question 2

Given an array arr = {12, 13, 35, 78, 56} and key = 78; How many iterations are done until the element is found using Binary search?

Cross

3

Tick

2

Cross

1

Cross

5



Question 2-Explanation: 

Given array: {12, 13, 35, 78, 56} Key: 78

1st iteration:

  • left = 0 (pointing to 12), right = 4 (pointing to 56), mid = (0 + 4) / 2 = 2, arr[mid] = 35 Since arr[mid] (35) is less than the key (78), we discard the left half of the array.

2nd iteration:

  • left = 3 (pointing to 78), right = 4 (pointing to 56), mid = (3 + 4) / 2 = 3, arr[mid] = 78 We found the key (78) at index 3.

So, it took 2 iterations to find the key (78) in the given array using binary search.

Hence (B) is the correct answer.

Question 3

Select the best description to explain what a binary search algorithm is.

Cross

Put the elements in order, check each item in turn.

Cross

Elements do not need to be in order, compare to the middle value, split the list in order and repeat

Cross

Elements do not need to be in order, check each item in turn.

Tick

Put the elements in order, compare with the middle value, split the list in order and repeat.



Question 4

What is the best-case TIme complexity of Binary search to find an element?

Cross

O(LogN)

Cross

O(N)

Cross

O(N^2)

Tick

O(1)



Question 4-Explanation: 

The best case is when the element is at the middle index of the array. It takes only one comparison to find the target element. So the best case complexity is O(1).

Hence Option(D) is the correct answer.


 

Question 5

A binary search is to be performed on the list:
3  5  7  10  23
How many comparisons would it take to find number 7?

Tick

0-1

Cross

3-4

Cross

2-3

Cross

can't find



Question 5-Explanation: 

1st iteration:

  • left = 0 (pointing to 3), right = 4 (pointing to 23), mid = (0 + 4) / 2 = 2, arr[mid] = 7 Since arr[mid] (7) is found at 1st iteration.

Hence Option(A) is the correct answer.


 

Question 6

What are the mid values (corresponding array items) produced in the first and second iterations for an array arr = [23, 45, 67, 89, 90,46 ]and key = 90?

Cross

67 and 89

Cross

67 and 46

Tick

67 and 90

Cross

None 



Question 6-Explanation: 

1st iteration:

  • left = 0 (pointing to 23), right = 5(pointing to 46), mid = (0 + 5) / 2 = 2, arr[mid] = 67 Since arr[mid] (67) is less than the key (90), we discard the left half of the array.

2nd iteration:

  • left = 3 (pointing to 89), right = 5(pointing to 46), mid = (3 + 5) / 2 = 4, arr[mid] = 90 We found the key (90) at index 2.

So, the first and second mid values are respectively 67 and 90.

Hence (C) is the correct answer.


 

Question 7

Identify what the below function is doing.

C++

int fun(int arr[], int size, int key, int k)
	{
		int s = 0;
		int e = size - 1;
		int mid = s + (e - s) / 2;
		int ans = -1;
		while (s <= e) {
			if (arr[mid] == key) {
				if (arr[mid - k] == arr[mid])
					e = mid - 1;
				else if (arr[mid - (k - 1)] == arr[mid]) {
					ans = mid;
					break;
				}
				else {
					s = mid + 1;
				}
			}
			else if (key < arr[mid]) {
				e = mid - 1;
			}
			else if (key > arr[mid]) {
				s = mid + 1;
			}
			mid = s + (e - s) / 2;
		}
		return ans;
	}


Cross

Performing Binary search

Cross

Finding Kth occurrence of the element  in O(N)

Tick

Finding the kth occurrence of the  element in O(logN)

Cross

None



Question 7-Explanation: 

The above function will find the index of Kth occurrence of X in the given array using O(LogN).

Below are the steps for the above approach:

  • Initialize the mid index, mid = start + (end – start) / 2, and a variable ans = -1.
  • Run a while loop till start ≤ end index,
  • Check if the key element and arr[mid] are the same, there is a possibility that the Kth occurrence of the key element is either on the left side or on the right side of the arr[mid]. 
    • Check if arr[mid-K] and arr[mid] are the same. Then the Kth occurrence of the key element must lie on the left side of the arr[mid]. 
    • Else if arr[mid-(K-1)] and arr[mid] are the same, then arr[mid] itself will be the Kth element, update ans = mid.
    • Else Kth occurrence of the element will lie on the right side of the mid-element. For which start becomes start = mid+1 (Going to the right search space)
  • If arr[mid] and key elements are not the same, then
    • Check if key < arr[mid], then end = mid – 1. Since the key element is smaller than arr[mid], it will lie to the left of arr[mid].
    • Check if key > arr[mid], then start = mid +1. Since the key element is greater than arr[mid], it will lie to the right of arr[mid].
  • Return the Kth occurrence of the key element.

Hence (C) is the correct answer.

Question 8

Which of the following is correct recurrence for worst case of Binary Search?

Cross

T(n) = 2T(n/2) + O(1) and T(1) = T(0) = O(1)

Cross

T(n) = T(n-1) + O(1) and T(1) = T(0) = O(1)

Tick

T(n) = T(n/2) + O(1) and T(1) = T(0) = O(1)

Cross

T(n) = T(n-2) + O(1) and T(1) = T(0) = O(1)



Question 8-Explanation: 

In binary Search, we first compare the given element x with middle of the array. If x matches with middle element, then we return middle index. Otherwise, we either recur for left half of array or right half of array. So recurrence is T(n) = T(n/2) + O(1)

Hence Option(C) is the correct answer

Question 9

Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.

C

1.   f(int Y[10], int x) {
2.     int i, j, k;
3.     i = 0; j = 9;
4.     do {
5.             k =  (i + j) /2;
6.             if( Y[k] < x)  i = k; else j = k;
7.         } while(Y[k] != x && i < j);
8.     if(Y[k] == x) printf ("x is in the array ") ;
9.     else printf (" x is not in the array ") ;
10. }

On which of the following contents of Y and x does the program fail?

Cross

Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even

Tick

Y is [2 2 2 2 2 2 2 2 2 2] and x > 2

Cross

Y is [1 3 5 7 9 11 13 15 17 19] and x < 1

Cross

Y is [1 2 3 4 5 6 7 8 9 10] and x < 10



Question 9-Explanation: 

The above program doesn’t work for the cases where the element to be searched is the last element of Y[] or greater than the last element (or maximum element) in Y[]. For such cases, the program goes in an infinite loop because i is assigned value as k in all iterations, and i never becomes equal to or greater than j. So while the condition never becomes false.

Hence Option (B) is the correct option

Question 10

The time taken by binary search algorithm to search a key in a sorted array of n elements is

Cross

O ( n log2n )

Cross

O ( n )

Cross

O ( n^2)

Tick

O(log2n)



Question 10-Explanation: 

Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. It takes a maximum of log(n) searches to search an element from the sorted array. 

Hence Option (D) is correct.

There are 12 questions to complete.


  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments
Similar Reads