Q. If a binary search is performed on a sorted array of size n, what is the space complexity?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n log n)
Solution
The space complexity of binary search is O(1) when implemented iteratively, as it uses a constant amount of space.
Correct Answer:
A
— O(1)
Learn More →
Q. In a binary search tree (BST), how does binary search differ from searching in a sorted array?
-
A.
It is slower
-
B.
It requires more comparisons
-
C.
It can be done in O(1)
-
D.
It uses tree properties
Solution
In a binary search tree, the search leverages the tree structure, allowing for efficient searching based on the properties of BSTs.
Correct Answer:
D
— It uses tree properties
Learn More →
Q. In binary search, if the target value is not found, what will be the return value?
-
A.
-1
-
B.
0
-
C.
null
-
D.
the index of the closest value
Solution
Typically, binary search returns -1 to indicate that the target value is not present in the array.
Correct Answer:
A
— -1
Learn More →
Q. What happens if binary search is applied to a linked list?
-
A.
It works efficiently
-
B.
It cannot be applied
-
C.
It works but is inefficient
-
D.
It requires additional data structures
Solution
Binary search cannot be efficiently applied to a linked list because it requires random access to elements, which linked lists do not provide.
Correct Answer:
C
— It works but is inefficient
Learn More →
Q. What is the maximum number of comparisons needed to find an element in an array of size 1024 using binary search?
Solution
The maximum number of comparisons is log2(1024) = 10, but since we start counting from 0, it takes 11 comparisons in the worst case.
Correct Answer:
B
— 11
Learn More →
Q. What is the result of performing binary search on an array with duplicate values?
-
A.
First occurrence of the value
-
B.
Last occurrence of the value
-
C.
Any occurrence of the value
-
D.
None of the above
Solution
Binary search can return any occurrence of the target value when duplicates are present, depending on the implementation.
Correct Answer:
C
— Any occurrence of the value
Learn More →
Q. Which of the following is a key advantage of binary search over linear search?
-
A.
Simplicity
-
B.
Efficiency
-
C.
Memory usage
-
D.
Flexibility
Solution
Binary search is more efficient than linear search, especially for large datasets, due to its O(log n) time complexity.
Correct Answer:
B
— Efficiency
Learn More →
Q. Which of the following modifications can be made to binary search to find the first occurrence of a target value?
-
A.
Change the comparison operator
-
B.
Use a different data structure
-
C.
Modify the mid-point calculation
-
D.
Continue searching left after finding the target
Solution
To find the first occurrence, continue searching to the left even after finding the target value.
Correct Answer:
D
— Continue searching left after finding the target
Learn More →
Showing 1 to 8 of 8 (1 Pages)