Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value in a sorted array.
DSA Javascript
function findFloor(arr, target) {
let low = 0, high = arr.length - 1;
let floor = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
return arr[mid];
} else if (arr[mid] < target) {
floor = arr[mid];
low = [1];
} else {
high = mid - 1;
}
}
return floor;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1 instead of mid + 1 causes infinite loop or wrong result.
Updating high instead of low when arr[mid] < target.
✗ Incorrect
When arr[mid] is less than target, we move low to mid + 1 to search the right half.
2fill in blank
mediumComplete the code to find the ceil value in a sorted array.
DSA Javascript
function findCeil(arr, target) {
let low = 0, high = arr.length - 1;
let ceil = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
return arr[mid];
} else if (arr[mid] > target) {
ceil = arr[mid];
high = [1];
} else {
low = mid + 1;
}
}
return ceil;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 instead of mid - 1 causes wrong search direction.
Updating low instead of high when arr[mid] > target.
✗ Incorrect
When arr[mid] is greater than target, we move high to mid - 1 to search the left half.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loops.
DSA Javascript
function binarySearch(arr, target) {
let low = 0, high = arr.length - 1;
while (low [1] high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes missing the last element and possibly wrong results.
Using '>' or '>=' reverses the logic and causes no iterations.
✗ Incorrect
The loop should continue while low is less than or equal to high to cover all elements.
4fill in blank
hardFill both blanks to create a function that returns both floor and ceil values for a target in a sorted array.
DSA Javascript
function findFloorAndCeil(arr, target) {
let low = 0, high = arr.length - 1;
let floor = -1, ceil = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < target) {
floor = arr[mid];
low = [1];
} else {
ceil = arr[mid];
high = [2];
}
}
return { floor, ceil };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high updates causes infinite loops.
Using low + 1 or high - 1 incorrectly without mid causes wrong results.
✗ Incorrect
When arr[mid] < target, move low to mid + 1; when arr[mid] > target, move high to mid - 1.
5fill in blank
hardFill all three blanks to create a function that returns floor and ceil using a single pass binary search.
DSA Javascript
function floorAndCeil(arr, target) {
let low = 0, high = arr.length - 1;
let floor = -1, ceil = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
floor = arr[mid];
ceil = arr[mid];
break;
} else if (arr[mid] < target) {
floor = arr[mid];
low = [1];
} else {
ceil = arr[mid];
high = [2];
}
}
return { [3] };
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low and high instead of floor and ceil.
Incorrect pointer updates causing infinite loops.
✗ Incorrect
Update low to mid + 1, high to mid - 1, and return floor and ceil as an object.