programming
beginner
10 sample questions
Searching Algorithms MCQ Practice Test
Linear Search: Simple but inefficient method of searching through a list.
Binary Search: Efficient search algorithm using divide-and-conquer strategy.
Hash Tables: Understanding how hash tables enable O(1) average time complexity for searches.
Q1. You are searching for the number 27 in a sorted array using a binary search. The array is: [5, 12, 18, 23, 27, 31, 40, 48]. In the *second* iteration of the binary search, what is the middle element being examined?
-
A. 12
-
B. 18
-
C. 23
-
D. 31 ✓
Explanation: Iteration 1: indices 0–7, midpoint (0+7)//2 = 3 → arr[3] = 23. Since 23 < 27, the search continues in the right half, indices 4–7. Iteration 2: midpoint (4+7)//2 = 5 → arr[5] = 31, so the middle element examined in the second iteration is 31. (Since 31 > 27, iteration 3 examines arr[4] = 27 and finds the target.) The old key '23' was iteration 1's midpoint; the stale note's claim of '27' was also wrong.
Q2. You are searching for the number 38 in a sorted array using binary search. The array is: [5, 12, 19, 27, 38, 51, 62]. In the second iteration of the algorithm, which portion of the array will be examined?
-
A. [5, 12, 19]
-
B. [19, 27, 38]
-
C. [38, 51, 62] ✓
-
D. [27, 38, 51, 62]
Explanation: First iteration: the middle element is 27 (index 3 of 7). Since 38 > 27, the left half including 27 is discarded, leaving [38, 51, 62] to be examined in the second iteration.
Q3. Consider a scenario where you need to search for a specific element within a massive, highly-dynamic dataset distributed across multiple servers. Each server holds a sorted subset of the data, and communication between servers incurs significant latency. Which search algorithm, when adapted for this distributed environment, would likely offer the best balance between minimizing inter-server communication and maintaining relatively efficient search times, assuming the data cannot be efficiently pre-processed for global indexing?
-
A. A parallel breadth-first search utilizing a distributed queue.
-
B. A modified distributed binary search employing a hierarchical query routing strategy. ✓
-
C. A consistent hashing approach coupled with a local linear search on each node.
-
D. A distributed bloom filter for initial candidate selection, followed by a local search.
Explanation: While parallel breadth-first search is viable, it can lead to excessive inter-server communication. Consistent hashing with local linear search is inefficient for large sorted datasets. A distributed bloom filter reduces false positives but necessitates a secondary search. A hierarchical binary search, intelligently routing queries based on data ranges across servers, minimizes communication by efficiently narrowing down the search space across the network.
Q4. Given a sorted array of integers containing duplicate values, which search algorithm is most suitable for efficiently finding the first or last occurrence of a target element?
-
A. Binary Search ✓
-
B. Interpolation Search
-
C. Exponential Search
-
D. Ternary Search
Explanation: Binary Search is the most suitable algorithm for efficiently finding the first or last occurrence of a target element in a sorted array with duplicates. By modifying the standard binary search algorithm, you can pinpoint the index of the first or last occurrence. Interpolation search's efficiency depends on the data distribution, and Exponential and Ternary search are not as well suited for this specific task.
Q5. In a binary search algorithm, what is the time complexity of finding an element in a sorted array when the search space is reduced by half at each step?
-
A. O(log n) ✓
-
B. O(n log n)
-
C. O(n^2)
-
D. O(2^n)
Explanation: In a binary search algorithm, the search space is reduced by half at each step, resulting in a time complexity of O(log n). This is because with each comparison, the search space is halved, leading to a logarithmic reduction in the number of comparisons required to find the element.
Q6. What is the time complexity of the Binary Search algorithm when the input array is already sorted in ascending order?
-
A. O(n)
-
B. O(log n) ✓
-
C. O(n log n)
-
D. O(n^2)
Explanation: Binary Search works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one. This is why the time complexity is O(log n).
Q7. Given a binary search tree with a large number of nodes, which of the following traversal strategies would result in the least amount of extra memory usage?
-
A. Depth-First Search (DFS) with recursive approach
-
B. Breadth-First Search (BFS) with iterative approach ✓
-
C. In-Order Traversal with stack data structure
-
D. Pre-Order Traversal with recursive approach
Explanation: BFS with iterative approach does not use recursion, which means it does not use the system stack. This results in the least amount of extra memory usage compared to the other options.
Q8. Given a sorted array of integers, what is the time complexity of the binary search algorithm when the target element is not present in the array?
-
A. O(log n) ✓
-
B. O(n)
-
C. O(n log n)
-
D. O(1)
Explanation: Binary search works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the next interval will be the lower half. Otherwise, the next interval will be the upper half. Ultimately, the search will either find the search value, or the interval will be empty, indicating that the value is not present in the array. The time complexity remains O(log n) even when the target element is not present, as the algorithm still divides the search space in half at each step.
Q9. Given a sorted array of integers, which of the following binary search implementations has a time complexity of O(log n) and does not require any additional space?
-
A. Iterative implementation using a while loop ✓
-
B. Recursive implementation with function calls
-
C. Implementation using a stack data structure
-
D. Implementation using a hash table for probing
Explanation: The iterative implementation of binary search using a while loop has a time complexity of O(log n) because it eliminates half of the search space in each iteration. It does not require any additional space, making it the most space-efficient solution.
Q10. In a binary search algorithm, what is the time complexity of finding an element in a sorted array of n elements?
-
A. O\(log^2 n\)
-
B. O\(n\)
-
C. O\(n log n\)
-
D. O\(log n\) ✓
Explanation: The binary search algorithm works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one. This process takes log n steps, making the time complexity O(log n).
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free