Challenge - 5 Problems
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linear Search on an Unsorted Array
What is the output of the following Java code that performs a linear search for the value 7 in an unsorted array?
DSA Java
public class SearchTest { public static int linearSearch(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; } } return -1; } public static void main(String[] args) { int[] data = {4, 2, 7, 1, 9}; System.out.println(linearSearch(data, 7)); } }
Attempts:
2 left
💡 Hint
Linear search returns the index of the first matching element.
✗ Incorrect
The value 7 is at index 2 in the array. Linear search checks each element from start until it finds 7 and returns its index.
❓ Predict Output
intermediateOutput of Binary Search on a Sorted Array
What is the output of the following Java code that performs a binary search for the value 5 in a sorted array?
DSA Java
import java.util.Arrays; public class SearchTest { public static int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] data = {1, 3, 5, 7, 9}; System.out.println(binarySearch(data, 5)); } }
Attempts:
2 left
💡 Hint
Binary search returns the index of the target if found in the sorted array.
✗ Incorrect
The value 5 is at index 2 in the sorted array. Binary search finds it by repeatedly dividing the search interval.
🧠 Conceptual
advancedTime Complexity Difference Between Linear and Binary Search
Which statement correctly describes the time complexity difference between linear search and binary search on an array of size n?
Attempts:
2 left
💡 Hint
Binary search divides the search space in half each step.
✗ Incorrect
Linear search checks each element one by one, so it takes O(n) time. Binary search divides the array in half each step, so it takes O(log n) time.
❓ Predict Output
advancedOutput of Binary Search on an Unsorted Array
What is the output of the following Java code that performs a binary search for the value 3 in an unsorted array?
DSA Java
public class SearchTest { public static int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] data = {1, 5, 4, 2, 3}; System.out.println(binarySearch(data, 3)); } }
Attempts:
2 left
💡 Hint
Binary search requires a sorted array to work correctly.
✗ Incorrect
The array is unsorted, so binary search cannot reliably find the target. It returns -1 indicating not found.
🧠 Conceptual
expertReal Cost Difference Between Binary and Linear Search in Practice
Which statement best explains the real cost difference between binary search and linear search when searching large sorted arrays?
Attempts:
2 left
💡 Hint
Consider overhead and data layout effects on performance.
✗ Incorrect
Binary search uses fewer comparisons but has overhead and requires sorted data. Linear search can be faster on small or nearly sorted data due to simpler logic and better cache use.
