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 Typescript
function linearSearch(arr: number[], target: number): number {
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
Comparing arr[i] with i instead of target
Returning the element instead of the index
Using assignment '=' instead of comparison '==='
✗ Incorrect
The linear search 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 and return the index of the target or -1 if not found.
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] [1] target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong search direction
Using '!=' or '===' in the else if condition
Not updating left or right correctly
✗ Incorrect
If the middle element is greater than the target, search the left half by moving right to mid - 1.
3fill in blank
hardFix the error in the binary search code to correctly calculate the middle index without overflow.
DSA Typescript
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = [1];
if (arr[mid] === target) {
return mid;
} else if (arr[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using (left + right) / 2 directly causing overflow
Using Math.ceil which can cause infinite loops
Using bitwise shift which may be unclear
✗ Incorrect
Using left + Math.floor((right - left) / 2) prevents overflow when left and right are large.
4fill in blank
hardComplete the code to create a function that compares the number of steps taken by linear and binary search for a target in a sorted array.
DSA Typescript
function compareSearchSteps(arr: number[], target: number): { linearSteps: number; binarySteps: number } {
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++;
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
break;
} else if (arr[mid] [1] target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return { linearSteps, binarySteps, };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in binary search comparison
Using semicolon instead of comma in object return
Not incrementing step counters
✗ Incorrect
5fill in blank
hardFill all three blanks to create a function that measures and prints the time taken by linear and binary search to find a target in a sorted array.
DSA Typescript
function measureSearchTime(arr: number[], target: number): void {
const startLinear = performance.now();
linearSearch(arr, target);
const endLinear = performance.now();
const startBinary = performance.now();
binarySearch(arr, target);
const endBinary = performance.now();
console.log('Linear Search Time: ' + (endLinear [1] startLinear) + ' ms');
console.log('Binary Search Time: ' + (endBinary [2] startBinary) + ' ms');
console.log('Binary search is approximately ' + ((endLinear [3] startLinear) / (endBinary [2] startBinary)).toFixed(2) + ' times faster');
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causing incorrect time calculation
Using multiplication or division instead of subtraction
Mixing operators inconsistently
✗ Incorrect
Subtract start time from end time to get elapsed time; use subtraction for both searches and for ratio calculation.