Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the floor of a number mean in a sorted array context?
easy
A. The greatest element less than or equal to the number
B. The smallest element greater than or equal to the number
C. The average of all elements less than the number
D. The smallest element strictly less than the number

Solution

  1. Step 1: Understand floor definition in sorted array

    The floor of a number is the greatest element in the array that is less than or equal to the given number.
  2. Step 2: Compare with options

    The greatest element less than or equal to the number matches this definition exactly, while others describe ceil or incorrect concepts.
  3. Final Answer:

    The greatest element less than or equal to the number -> Option A
  4. Quick Check:

    Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor must be strictly less than number
  • Mixing floor with average or median
2. Which of the following is the correct JavaScript syntax to find the floor of a number x in a sorted array arr using binary search?
easy
A. Use Math.floor(x) directly on array
B. Use a for loop from 0 to arr.length and return first element greater than x
C. Use arr.indexOf(x) to find floor
D. Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x

Solution

  1. Step 1: Understand binary search for floor

    Binary search uses start and end pointers, calculates mid, and checks if arr[mid] <= x to move start or end accordingly.
  2. Step 2: Evaluate options

    Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x correctly describes binary search logic for floor. Options A, B, and C are incorrect or irrelevant.
  3. Final Answer:

    Use a while loop with start=0, end=arr.length-1, update mid and check arr[mid] ≤ x -> Option D
  4. Quick Check:

    Binary search with arr[mid] ≤ x finds floor [OK]
Hint: Binary search with arr[mid] ≤ x finds floor efficiently [OK]
Common Mistakes:
  • Using linear search instead of binary search
  • Using indexOf which only finds exact matches
  • Misusing Math.floor which works on numbers, not arrays
3. What is the output of the following JavaScript code?
const arr = [1, 3, 5, 7, 9];
function findFloor(arr, x) {
  let start = 0, end = arr.length - 1, floor = -1;
  while (start <= end) {
    let mid = Math.floor((start + end) / 2);
    if (arr[mid] <= x) {
      floor = arr[mid];
      start = mid + 1;
    } else {
      end = mid - 1;
    }
  }
  return floor;
}
console.log(findFloor(arr, 6));
medium
A. 6
B. 5
C. 7
D. -1

Solution

  1. Step 1: Trace binary search for floor of 6

    Initial start=0, end=4, floor=-1. mid=2 (arr[2]=5) ≤ 6, floor=5, start=3. mid=3 (arr[3]=7) > 6, end=2. Loop ends.
  2. Step 2: Return floor value

    Floor found is 5, which is the greatest element ≤ 6.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Floor of 6 in [1,3,5,7,9] is 5 [OK]
Hint: Floor is last arr[mid] ≤ x during binary search [OK]
Common Mistakes:
  • Returning mid index instead of value
  • Confusing floor with ceil
  • Not updating floor variable correctly
4. Identify the bug in this JavaScript function to find the ceil of a number x in a sorted array arr:
function findCeil(arr, x) {
  let start = 0, end = arr.length - 1, ceil = -1;
  while (start <= end) {
    let mid = Math.floor((start + end) / 2);
    if (arr[mid] >= x) {
      ceil = arr[mid];
      end = mid + 1;
    } else {
      start = mid + 1;
    }
  }
  return ceil;
}
medium
A. The line end = mid + 1; should be end = mid - 1;
B. The line start = mid + 1; should be start = mid - 1;
C. The initial value of ceil should be 0 instead of -1
D. The while loop condition should be start < end instead of start <= end

Solution

  1. Step 1: Analyze binary search update for ceil

    When arr[mid] ≥ x, we update ceil and must move end to mid - 1 to search left side for smaller ceil.
  2. Step 2: Identify incorrect update

    The code incorrectly sets end = mid + 1, which moves end right, causing infinite loop or wrong result.
  3. Final Answer:

    The line end = mid + 1; should be end = mid - 1; -> Option A
  4. Quick Check:

    For ceil, move end left with end = mid - 1 [OK]
Hint: For ceil, move end left; for floor, move start right [OK]
Common Mistakes:
  • Moving end pointer in wrong direction
  • Confusing ceil logic with floor
  • Incorrect initial ceil value
5. Given a sorted array arr = [2, 4, 6, 8, 10], write a function to find both floor and ceil of a number x. What will be the floor and ceil of x = 5?
hard
A. Floor = 5, Ceil = 5
B. Floor = 6, Ceil = 4
C. Floor = 4, Ceil = 6
D. Floor = 2, Ceil = 10

Solution

  1. Step 1: Understand floor and ceil definitions

    Floor is greatest element ≤ x, ceil is smallest element ≥ x.
  2. Step 2: Find floor and ceil for x=5 in arr

    Elements ≤ 5 are 2 and 4; greatest is 4 (floor). Elements ≥ 5 are 6, 8, 10; smallest is 6 (ceil).
  3. Final Answer:

    Floor = 4, Ceil = 6 -> Option C
  4. Quick Check:

    Floor ≤ 5 = 4, Ceil ≥ 5 = 6 [OK]
Hint: Floor ≤ x max, Ceil ≥ x min in sorted array [OK]
Common Mistakes:
  • Swapping floor and ceil values
  • Assuming floor or ceil equals x if not present
  • Choosing wrong boundary elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard