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 TypeScript code that performs a linear search for the number 7 in an unsorted array?
DSA Typescript
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
const array = [3, 1, 4, 7, 5];
console.log(linearSearch(array, 7));Attempts:
2 left
💡 Hint
Remember linear search checks elements one by one from the start.
✗ Incorrect
The linear search checks each element until it finds 7 at index 3 (0-based). So it returns 3.
❓ Predict Output
intermediate2:00remaining
Output of Binary Search on a Sorted Array
What is the output of the following TypeScript code that performs a binary search for the number 7 in a sorted array?
DSA Typescript
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
const array = [1, 3, 4, 5, 7, 9];
console.log(binarySearch(array, 7));Attempts:
2 left
💡 Hint
Binary search divides the array and finds the target index.
✗ Incorrect
The number 7 is at index 4 in the sorted array, so binary search returns 4.
🧠 Conceptual
advanced2:00remaining
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
Think about how many elements each search checks in the worst case.
✗ Incorrect
Binary search cuts the search space in half each time, so it runs in logarithmic time, which is much faster than linear search's checking every element.
❓ Predict Output
advanced2:00remaining
Output of Binary Search on an Unsorted Array
What is the output of the following TypeScript code that performs a binary search for the number 7 on an unsorted array?
DSA Typescript
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
const array = [8, 9, 10, 1, 7];
console.log(binarySearch(array, 7));Attempts:
2 left
💡 Hint
Binary search requires a sorted array to work correctly.
✗ Incorrect
Since the array is unsorted, binary search cannot reliably find the target and returns -1.
🧠 Conceptual
expert2:00remaining
When Linear Search Can Be More Efficient Than Binary Search
In which scenario can linear search be more efficient than binary search despite binary search's better average time complexity?
Attempts:
2 left
💡 Hint
Think about the overhead of binary search and the position of the target.
✗ Incorrect
For small arrays or when the target is near the start, linear search can find the target faster because it checks elements sequentially without overhead.