Data Structures & Algorithms Multiple Choice Questions on “Linear Search Recursive”.
1. Is there any difference in the speed of execution between linear serach(recursive) vs linear search(lterative)? Answer: c 2. Is the space consumed by the linear search(recursive) and linear search(iterative) same? Answer: a 3. What is the worst case runtime of linear search(recursive) algorithm? Answer: a 4. Linear search(recursive) algorithm used in _____________ Answer: a 5. The array is as follows: 1,2,3,6,8,10. At what time the element 6 is found? (By using linear search(recursive) algorithm) Answer: a 6. The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched. At which call it tells that there’s no such element? (By using linear search(recursive) algorithm) Answer: a 7. What is the best case runtime of linear search(recursive) algorithm on an ordered set of elements? Answer: a 8. Which of the following code snippet performs linear search recursively? b) c) d) Answer: b 9. Can linear search recursive algorithm and binary search recursive algorithm be performed on an unordered list? 10. What is the recurrence relation for the linear search recursive algorithm?
a) Both execute at same speed
b) Linear search(recursive) is faster
c) Linear search(Iterative) is faster
d) Cant be said
Clarification: The Iterative algorithm is faster than the latter as recursive algorithm has overheads like calling function and registering stacks repeatedly.
a) No, recursive algorithm consumes more space
b) No, recursive algorithm consumes less space
c) Yes
d) Nothing can be said
Clarification: The recursive algorithm consumes more space as it involves the usage the stack space(calls the function numerous times).
a) O(n)
b) O(logn)
c) O(n2)
d) O(nx)
Clarification: In the worst case scenario, there might be a need of calling the stack n times. Therfore O(n).
a) When the size of the dataset is low
b) When the size of the dataset is large
c) When the dataset is unordered
d) Never used
Clarification: It is used when the size of the dataset is low as its runtime is O(n) which is more when compared to the binary search O(logn).
a) 4th call
b) 3rd call
c) 6th call
d) 5th call
Clarification: Provided that the search starts from the first element, the function calls itself till the element is found. In this case, the element is found in 4th call.
a) 7th call
b) 9th call
c) 17th call
d) The function calls itself infinite number of times
Clarification: The function calls itself till the element is found. But at the 7th call it terminates as goes outside the array.
a) O(1)
b) O(n)
c) O(logn)
d) O(nx)
Clarification: The best case occurs when the given element to be found is at the first position. Therefore O(1) is the correct answer.
a) for(i=0;i<n;i++)
{
if(a[i]==key)
printf("element found");
}
LinearSearch(int[] a, n,key)
{
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n-1,key)
}
LinearSearch(int[] a, n,key)
{
if(n<1)
return True
if(a[n]==key)
return False
else
LinearSearch(a,n-1,key)
}
LinearSearch(int[] a, n,key)
{
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n+1,key)
}
Clarification: Compare n with first element in arr[]. If element is found at first position, return it. Else recur for remaining array and n.
a) Binary search can’t be used
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used
Answer: a
Clarification: As binary search requires comparison, it is required that the list be ordered. Whereas this doesn’t matter for linear search.
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
Answer: c
Clarification: After each call in the recursive algorithm, the size of n is reduced by 1. Therefore the optimal solution is T(n-1)+c.