0
0
DSA Javascriptprogramming~20 mins

Floor and Ceil in Sorted Array in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find floor and ceil of 7 in a sorted array
What is the output of the following code that finds the floor and ceil of 7 in the sorted array [1, 3, 5, 7, 9, 11]?
DSA Javascript
function floorAndCeil(arr, x) {
  let floor = -1, ceil = -1;
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === x) {
      floor = arr[mid];
      ceil = arr[mid];
      break;
    } else if (arr[mid] < x) {
      floor = arr[mid];
      left = mid + 1;
    } else {
      ceil = arr[mid];
      right = mid - 1;
    }
  }
  return { floor, ceil };
}

console.log(floorAndCeil([1, 3, 5, 7, 9, 11], 7));
A{"floor":7,"ceil":7}
B{"floor":5,"ceil":9}
C{"floor":6,"ceil":8}
D{"floor":-1,"ceil":-1}
Attempts:
2 left
💡 Hint
Remember floor is the greatest element less than or equal to x, ceil is the smallest element greater than or equal to x.
Predict Output
intermediate
2:00remaining
Floor and ceil for 8 in a sorted array
What is the output of the code that finds floor and ceil of 8 in the sorted array [2, 4, 6, 10, 12]?
DSA Javascript
function floorAndCeil(arr, x) {
  let floor = -1, ceil = -1;
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === x) {
      floor = arr[mid];
      ceil = arr[mid];
      break;
    } else if (arr[mid] < x) {
      floor = arr[mid];
      left = mid + 1;
    } else {
      ceil = arr[mid];
      right = mid - 1;
    }
  }
  return { floor, ceil };
}

console.log(floorAndCeil([2, 4, 6, 10, 12], 8));
A{"floor":6,"ceil":10}
B{"floor":10,"ceil":12}
C{"floor":8,"ceil":8}
D{"floor":4,"ceil":6}
Attempts:
2 left
💡 Hint
8 is not in the array, so floor and ceil are neighbors around 8.
🧠 Conceptual
advanced
1:30remaining
Floor and Ceil when element is smaller than all array elements
If you search for floor and ceil of 1 in the sorted array [3, 5, 7, 9], what will be the floor and ceil values?
A{"floor":3,"ceil":5}
B{"floor":1,"ceil":3}
C{"floor":-1,"ceil":3}
D{"floor":-1,"ceil":-1}
Attempts:
2 left
💡 Hint
Floor is the greatest element less than or equal to x. If none exists, floor is -1.
🧠 Conceptual
advanced
1:30remaining
Floor and Ceil when element is greater than all array elements
What are the floor and ceil of 20 in the sorted array [2, 4, 6, 8, 10]?
A{"floor":20,"ceil":20}
B{"floor":-1,"ceil":10}
C{"floor":8,"ceil":10}
D{"floor":10,"ceil":-1}
Attempts:
2 left
💡 Hint
Ceil is the smallest element greater than or equal to x. If none exists, ceil is -1.
🔧 Debug
expert
2:30remaining
Identify the error in floor and ceil implementation
What error will the following code produce when finding floor and ceil of 5 in [1, 2, 3, 4]?
DSA Javascript
function floorAndCeil(arr, x) {
  let floor = -1, ceil = -1;
  let left = 0, right = arr.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === x) {
      floor = arr[mid];
      ceil = arr[mid];
      break;
    } else if (arr[mid] < x) {
      floor = arr[mid];
      left = mid + 1;
    } else {
      ceil = arr[mid];
      right = mid - 1;
    }
  }
  return { floor, ceil };
}

console.log(floorAndCeil([1, 2, 3, 4], 5));
AThe code runs and returns { floor: -1, ceil: -1 }
BThe code runs infinitely (infinite loop)
CThe code runs and returns { floor: 3, ceil: 4 }
DThe code runs and returns { floor: 4, ceil: -1 }
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.