Challenge - 5 Problems
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Linear Search on an Unsorted Array
What is the output of the following JavaScript code that performs a linear search for the number 7 in an unsorted array?
DSA Javascript
const arr = [3, 1, 4, 7, 5]; function linearSearch(array, target) { for (let i = 0; i < array.length; i++) { if (array[i] === target) { return i; } } return -1; } console.log(linearSearch(arr, 7));
Attempts:
2 left
💡 Hint
Remember linear search checks each element until it finds the target.
✗ Incorrect
The function checks each element in order. The number 7 is at index 3 in the array, so it returns 3.
❓ Predict Output
intermediate2:00remaining
Output of Binary Search on a Sorted Array
What is the output of the following JavaScript code that performs a binary search for the number 7 in a sorted array?
DSA Javascript
const arr = [1, 3, 4, 5, 7, 9]; function binarySearch(array, target) { let left = 0; let right = array.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (array[mid] === target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } console.log(binarySearch(arr, 7));
Attempts:
2 left
💡 Hint
Binary search finds the middle element and narrows down the search.
✗ Incorrect
The number 7 is at index 4 in the sorted array, so the binary search returns 4.
🧠 Conceptual
advanced2:00remaining
Why Binary Search Requires a Sorted Array
Why does binary search require the array to be sorted to work correctly?
Attempts:
2 left
💡 Hint
Think about how binary search decides where to look next.
✗ Incorrect
Binary search compares the target with the middle element and decides to search left or right half. This logic only works if the array is sorted.
❓ Predict Output
advanced2:00remaining
Output of Binary Search on an Unsorted Array
What is the output of the following JavaScript code that performs a binary search for the number 7 in an unsorted array?
DSA Javascript
const arr = [7, 3, 1, 4, 5]; function binarySearch(array, target) { let left = 0; let right = array.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (array[mid] === target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } console.log(binarySearch(arr, 7));
Attempts:
2 left
💡 Hint
Binary search assumes the array is sorted, but here it is not.
✗ Incorrect
Because the array is not sorted, binary search cannot reliably find the target. It returns -1 indicating not found.
🧠 Conceptual
expert3:00remaining
Real Cost Difference Between Linear and Binary Search
Which statement best describes the real cost difference between linear search and binary search when searching for an element in a large sorted array?
Attempts:
2 left
💡 Hint
Consider the cost of sorting and repeated searches.
✗ Incorrect
Binary search requires the array to be sorted, which can be costly initially. However, for repeated searches, binary search is much faster than linear search because it halves the search space each time.