Q. How can binary search be modified to find the first occurrence of a target value in a sorted array with duplicates?
-
A.
Use a linear search
-
B.
Modify the mid-point logic
-
C.
Use a stack
-
D.
Use a queue
Solution
By modifying the mid-point logic to continue searching in the left half even after finding the target.
Correct Answer:
B
— Modify the mid-point logic
Learn More →
Q. What happens if you apply binary search on a sorted array with duplicate elements?
-
A.
It finds the first occurrence
-
B.
It finds the last occurrence
-
C.
It can find any occurrence
-
D.
It fails
Solution
Binary search can find any occurrence of the element, but it does not guarantee which one.
Correct Answer:
C
— It can find any occurrence
Learn More →
Q. Which of the following is a valid application of binary search?
-
A.
Finding the square root of a number
-
B.
Finding an element in a sorted array
-
C.
Sorting an array
-
D.
Searching in a hash table
Solution
Binary search is specifically used for finding an element in a sorted array.
Correct Answer:
B
— Finding an element in a sorted array
Learn More →
Showing 1 to 3 of 3 (1 Pages)