Data Structure Multiple Choice Questions on “Binary Search Iterative”.
1. What is the advantage of recursive approach than an iterative approach? Answer: b 2. Choose the appropriate code that does binary search using recursion. b) c) d) Answer: a 3. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion? Answer: c 4. Given an array arr = {45,77,89,90,94,99,100} and key = 99; what are the mid values(corresponding array elements) in the first and second levels of recursion? Answer: a 5. What is the worst case complexity of binary search using recursion? Answer: b 6. What is the average case time complexity of binary search using recursion? Answer: b 7. Which of the following is not an application of binary search? Answer: d 8. Choose among the following code for an iterative binary search. b) c) d) Answer: b 9. Binary Search can be categorized into which of the following? Answer: b 10. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the element is found? Answer: d 11. Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the mid values(corresponding array elements) generated in the first and second iterations? Answer: a 12. What is the time complexity of binary search with iteration? Answer: b
a) Consumes less memory
b) Less code and easy to implement
c) Consumes more memory
d) More code has to be written
Clarification: A recursive approach is easier to understand and contains fewer lines of code.
a)public static int recursive(int arr[], int low, int high, int key)
{
int mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid+1,high,key);
}
else
{
return recursive(arr,low,mid-1,key);
}
}
public static int recursive(int arr[], int low, int high, int key)
{
int mid = low + (high + low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid-1,high,key);
}
else
{
return recursive(arr,low,mid+1,key);
}
}
public static int recursive(int arr[], int low, int high, int key)
{
int mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid,high,key);
}
else
{
return recursive(arr,low,mid-1,key);
}
}
public static int recursive(int arr[], int low, int high, int key)
{
int mid = low + ((high - low)/2)+1;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid,high,key);
}
else
{
return recursive(arr,low,mid-1,key);
}
}
Clarification: Calculate the ‘mid’ value, and check if that is the key, if not, call the function again with 2 sub arrays, one with till mid-1 and the other starting from mid+1.
a) 5
b) 2
c) 3
d) 4
Clarification: level 1: mid = 7
level 2: mid = 99
level 3: mid = 899(this is the key).
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94
Clarification: At first level key = 90
At second level key= 99
Here 90 and 99 are mid values.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Clarification: Using the divide and conquer master theorem.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Clarification: T(n) = T(n/2) + 1, Using the divide and conquer master theorem.
a) To find the lower/upper bound in an ordered sequence
b) Union of intervals
c) Debugging
d) To search in unordered list
View Answer
Clarification: In Binary search, the elements in the list should be sorted. It is applicable only for ordered list. Hence Binary search in unordered list is not an application.
a)public static int iterative(int arr[], int key)
{
int low = 0;
int mid = 0;
int high = arr.length-1;
while(low <= high)
{
mid = low + (high + low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
low = mid - 1;
}
else
{
high = mid + 1;
}
}
return -1;
}
public static int iterative(int arr[], int key)
{
int low = 0;
int mid = 0;
int high = arr.length-1;
while(low <= high)
{
mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
public static int iterative(int arr[], int key)
{
int low = 0;
int mid = 0;
int high = arr.length-1;
while(low <= high)
{
mid = low + (high + low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
public static int iterative(int arr[], int key)
{
int low = 0;
int mid = 0;
int high = arr.length-1;
while(low <= high)
{
mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
low = mid - 1;
}
else
{
high = mid + 1;
}
}
return -1;
}
Clarification: Find the ‘mid’, check if it equals the key, if not, continue the iterations until low <= high.
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
Clarification: Since ‘mid’ is calculated for every iteration or recursion, we are diving the array into half and then try to solve the problem.
a) 1
b) 3
c) 4
d) 2
Clarification: Iteration1 : mid = 77; Iteration2 : mid = 88;
a) 90 and 99
b) 90 and 100
c) 89 and 94
d) 94 and 99
Clarification: Trace the input with the binary search iterative code.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Clarification: T(n) = T(n/2) + theta(1)
Using the divide and conquer master theorem, we get the time complexity as O(logn).