Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform a linear search that returns the index of the target or -1 if not found.
DSA Javascript
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === [1]) {
return i;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop index 'i' instead of the target for comparison.
Comparing arr[i] with arr instead of target.
✗ Incorrect
The function compares each element arr[i] with the target value to find a match.
2fill in blank
mediumComplete the code to perform a binary search on a sorted array to find the target index or return -1 if not found.
DSA Javascript
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === [1]) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using left or right instead of target for comparison.
✗ Incorrect
The binary search compares the middle element arr[mid] with the target to decide the search direction.
3fill in blank
hardFix the error in the binary search code to correctly update the search boundaries.
DSA Javascript
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = [1];
} else {
right = mid - 1;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting left to mid - 1 which moves left boundary incorrectly.
Using right - 1 or left + 1 which are not correct updates here.
✗ Incorrect
When arr[mid] is less than target, we move the left boundary to mid + 1 to search the right half.
4fill in blank
hardFill both blanks to create a function that compares the number of steps taken by linear and binary search to find a target.
DSA Javascript
function compareSearches(arr, target) {
let linearSteps = 0;
for (let i = 0; i < arr.length; i++) {
linearSteps++;
if (arr[i] === target) {
break;
}
}
let left = 0;
let right = arr.length - 1;
let binarySteps = 0;
while (left <= right) {
binarySteps++;
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
break;
} else if (arr[mid] < target) {
left = [1];
} else {
right = [2];
}
}
return { linearSteps, binarySteps };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the updates of left and right boundaries.
Using left + 1 or right - 1 incorrectly.
✗ Incorrect
In binary search, when arr[mid] < target, left moves to mid + 1; when arr[mid] > target, right moves to mid - 1.
5fill in blank
hardFill all three blanks to create a function that returns the search method with fewer steps for a given target.
DSA Javascript
function bestSearchMethod(arr, target) {
const { linearSteps, binarySteps } = compareSearches(arr, target);
if (linearSteps [1] binarySteps) {
return [2];
} else {
return [3];
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in the if condition.
Returning the wrong search method string.
✗ Incorrect
If linearSteps is less than binarySteps, return "Linear Search"; otherwise, return "Binary Search".