DS On Search
DS On Search
2. Select the code snippet which performs unordered linear search iteratively?
3. What is the best case for linear search and worst case ?
a) O(nlogn)
b) O(logn)
c) O(n) (worst)
d) O(1) (best)
4. Select the code snippet which performs ordered linear search iteratively?
public int linearSearch(int arr[],int key,int size) {
int index = -1; int i = 0;
while(size > 0) {
if(data[i] == key) {
index = i; }
if(data[i] > key)) { break; }
i++; } return index;}
5. Choose the code snippet which uses recursion for linear search.
public void linSearch(int[] arr, int first, int last, int key){
if(first == last)
{ System.out.print("-1");}
else
{ if(arr[first] == key) {
System.out.print(first);
}
else
{
linSearch(arr, first+1, last, key);
}
}}
6. What does the following piece of code do?
LinearSearch(int[] a, n,key)
{
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n-1,key)
}
7. Can linear search recursive algorithm and binary search recursive algorithm be
performed on an unordered list?
a) Binary search can’t be used, requires comparison and be ordered
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used
7. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
.
2. . Choose the appropriate code that does binary search using recursion.
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);
}}
3. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
a) 5
b) 2
c) 3
d) 4
Explanation: level 1: mid = 7
level 2: mid = 99
level 3: mid = 899(this is the key).
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?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94
5. What is the worst and average case complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Explanation: T(n) = T(n/2) + 1, Using the divide and conquer master theorem.
8. Binary Search can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming
1. In which of the cases uniform binary search fails compared to binary search?
a) A table lookup is generally faster than an addition and a shift
b) Many searches will be performed on the same array
c) Many searches will be performed on several arrays of the same length
d) Complexity of code
2. Which of the following is a suitable lookup table that can be used in the uniform
binary search?(N is the number of elements in the array and the delta array is global)
public static void make_delta(int N) {
int power = 1;
int i = 0;
do
{
int half = power;
power <<= 1;
delta[i] = (N + half) / power;
}
while (delta[i++] != 0);}
3. Given delta[4] is a global array and number of elements in the sorted array is 10,
what are the values in the delta array?
a) 4, 3, 1, 0
b) 5, 3, 1, 0
c) 4, 2, 1, 1
d) 5, 2, 1, 1
4. Choose the appropriate code snippet that performs uniform binary search.
Therefore, the correct answer is option b) 3 (since the algorithm only makes one key
comparison excluding the comparison used to decide the left or right subarray).
Explanation: Linear search has O(n) complexity and Binary search has O(logn)
complexity, in Jump search you have to jump backwards only once, hence it is preferable
if jumping backwards is costly. Jump search starts from index k (specified index) and
searches for the element. It won’t start searching from index 0.
3. Which of the following step is taken after finding an element having value greater
than the element being searched?
a) linear search takes place in the forward direction
b) linear search takes place in the backward direction
c) binary search takes place in the forward direction
d) binary search takes place in a backward direction
4. How many jumps will be made in the worst case of jump search(let block jumped =k)?
a) n*k
b) n/k
c) k/n
d) n+k
5. What will be the maximum number of comparisons that can be made in jump search
algorithm (assuming k to be blocks jumped)?
a) k
b) n/k
c) k-1
d) k-1
6. What is the value of jump taken for maximum efficiency while implementing jump
search? Total comparisons n/k+k-1
a) n/2
b) n2
c) sqrt(n)
d) log n
9. In which of the following case jump search will be preferred over binary search?
a) jumping backwards takes significantly more time than jumping forward
b) jumping forward takes significantly more time than jumping backwards
c) when the given array is very large in size
d) when the given array is very small in size
10. Which of the following code correctly represent jump search?
11. Jump search is worse than linear search in terms of time complexity.
a) True
b) False
Explanation: Linear search has a time complexity of O(n) and the time complexity of
jump search is O(n1/2). So jump search is better than linear search in terms of time
complexity.
2. Which of the following searching algorithm is used with exponential sort after finding
the appropriate range?
a) Linear search
b) Binary search
c) Jump search
d) Fibonacci Search
4. Choose the correct while loop statement from the following that finds the range
where are the element being search is present (x is the element being searched in an
array arr of size n)?
while (i < n && arr[i] <= x)
i = i*2;
Explanation: In exponential search we first find the range where the element being
searched can be present before applying binary search. We do this by comparing the
value of element under search with the array elements present at the positions
1,2,4,8….n.
5. What is the time complexity of exponential sort?
a) O(n)
b) O(2n)
c) O(n log n)
d) O(log n)
6. What is the auxiliary space requirement of the exponential sort when used with
recursive binary search?
a) O(n)
b) O(2n)
c) O(1)
d) O(log n)
8. In which of the following case jump search will be preferred over exponential search?
a) jumping backwards takes significantly more time than jumping forward
b) jumping forward takes significantly more time than jumping backwards
c) when the given array is very large in size
d) when the given array is very small in size
Explanation: Jump search only needs to jump backwards once, while an exponential
search can jump backwards up to log n times. Thus jump search will be preferred if
jumping backwards is expensive.
if (arr[0] == x)
return 0;
int i = 1;
i = i*2;
return binarySearch(arr, i/2, min(i, n-1), x);//applies binary search in the calculated
range}
10. Jump search has a better time complexity than the exponential search.
a) True
b) False
Answer: b
Explanation: The worst case time complexity of jump search and exponential searches
are O(n1/2) and O(log n) respectively. So exponential search is better in terms of time
complexity.
11. Exponential search performs better than binary search when the element being
searched is present near the starting point of the array.
a) True
b) False
Explanation: Exponential search first finds the range where binary search needs to be
applied. So when the element is present near the starting point of the array then
exponential search performs better than standard binary search.
14. Choose the incorrect statement about exponential search from the following.
a) Exponential search is an in place algorithm
b) Exponential search has a greater time complexity than binary search
c) Exponential search performs better than binary search when the element being
searched is present near the starting point of the array
d) Jump search has a greater time complexity than an exponential search
Explanation: Time complexity of exponential search and binary search are the
same. But exponential search performs better than binary search when the
element being searched is present near the starting point of the array.
1. Which of the following is the most desirable condition for interpolation search?
a) array should be sorted
b) array should not be sorted but the values should be uniformly distributed
c) array should have a less than 64 elements
d) array should be sorted and the values should be uniformly distributed
4. In which of the following case jump search performs better than interpolation search?
a) When array has uniformly distributed values but is not sorted
b) when array is sorted and has uniform distribution of values
c) when array is sorted but the values increases exponentially
d) when array is not sorted
Answer: c
Explanation: In case of non uniform distribution of values the time complexity of
interpolation search is O(n) whereas the average time complexity of jump search is
O(n1/2). So in such a case jump search has a better performance.
5. What is the time complexity of interpolation search when the input array has
uniformly distributed values and is sorted?
a) O(n)
b) O(log log n)
c) O(n log n)
d) O(log n)
6. What is the time complexity of exponential search when the input array is sorted but
the values are not uniformly distributed?
a) O(n1/2)
b) O(log log n)
c) O(n)
d) O(log n)
7. Which of the following searching algorithm is fastest when the input array is sorted
but has non uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search
8. Which of the following searching algorithm is fastest when the input array is not
sorted but has uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search
10. Interpolation search has a better time complexity than exponential search for any
given array.
a) True
b) False
Answer: b
Explanation: The worst case time complexity of interpolation search and exponential
search are O(n) and O(log n) respectively. So exponential search is better when the
worst case scenario is considered.
11. What are the updated values of high and low in the array if the element being
searched is greater than the value at calculated index in interpolation search? (pos =
current position)
a) low = pos + 1, high remains unchanged
b) high = pos – 1, low remains unchanged
c) low = low +1, high = high – 1
d) low = pos +1, high = pos – 1
3. What will be the worst case time complexity of the following code?
#include<bits/stdc++.h>
using namespace std;
void func(char* str2, char* str1) {
int m = strlen(str2);
int n = strlen(str1);
for (int i = 0; i <= n - m; i++)
{
int j;
for (j = 0; j < m; j++)
if (str1[i + j] != str2[j])
break;
if (j == m)
cout << i << endl;
}}
int main() {
char str1[] = "1253234";
char str2[] = "323";
func(str2, str1);
return 0; }
a) O(n)
b) O(m)
c) O(m * n)
d) O(m + n)
Explanation: The given code describes the naive method of pattern searching. By
observing the nested loop in the code we can say that the time complexity of the loop is
O(m*n).
4. What is the worst case time complexity of KMP algorithm for pattern searching (m =
length of text, n = length of pattern)?
a) O(n)
b) O(n*m)
c) O(m)
d) O(log n)
5. What will be the best case time complexity of the following code?
#include<bits/stdc++.h>
using namespace std; void func(char* str2, char* str1) {
int m = strlen(str2);
int n = strlen(str1);
for (int i = 0; i <= n - m; i++)
{
int j;
for (j = 0; j < m; j++)
if (str1[i + j] != str2[j])
break;
if (j == m)
cout << i << endl;
}}
int main() {
char str1[] = "1253234";
char str2[] = "323";
func(str2, str1);
return 0; }
a) O(n)
b) O(m)
c) O(m * n)
d) O(m + n)
6. What is the time complexity of Z algorithm for pattern searching (m = length of text, n
= length of pattern)?
a) O(n + m)
b) O(m)
c) O(n)
d) O(m * n)
9. Rabin Karp algorithm and naive pattern searching algorithm have the same worst
case time complexity.
a) true
b) false
Explanation: The worst case time complexity of Rabin Karp algorithm is O(m*n) but it
has a linear average case time complexity. So Rabin Karp and naive pattern searching
algorithm have the same worst case time complexity.