0
0
DSA Javascriptprogramming~10 mins

Floor and Ceil in Sorted Array in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Alow + 1
Bmid + 1
Cmid - 1
Dhigh - 1
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.
2fill in blank
medium

Complete 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'
Amid - 1
Blow - 1
Chigh + 1
Dmid + 1
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.
3fill in blank
hard

Fix 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'
A<=
B<
C>=
D>
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.
4fill in blank
hard

Fill 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'
Amid + 1
Bmid - 1
Clow + 1
Dhigh - 1
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.
5fill in blank
hard

Fill 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'
Amid + 1
Bmid - 1
Cfloor, ceil
Dlow, high
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low and high instead of floor and ceil.
Incorrect pointer updates causing infinite loops.