0
0
DSA Javascriptprogramming~10 mins

Floor and Ceil in BST 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 start searching for the floor value in the BST.

DSA Javascript
function floorInBST(root, key) {
  let floor = null;
  let current = [1];
  while (current !== null) {
    if (current.val === key) {
      floor = current.val;
      break;
    } else if (current.val > key) {
      current = current.left;
    } else {
      floor = current.val;
      current = current.right;
    }
  }
  return floor;
}
Drag options to blanks, or click blank then click option'
Aroot.left
Bkey
Cnull
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root.
Starting from root.left which skips the root node.
2fill in blank
medium

Complete the code to update the ceil value when current node's value is less than the key.

DSA Javascript
function ceilInBST(root, key) {
  let ceil = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      ceil = current.val;
      break;
    } else if (current.val < key) {
      current = current.right;
    } else {
      ceil = [1];
      current = current.left;
    }
  }
  return ceil;
}
Drag options to blanks, or click blank then click option'
Acurrent.val
Bkey
Ccurrent.right
Droot.val
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning ceil to current.right which is a node, not a value.
Assigning ceil to key which does not update the candidate.
3fill in blank
hard

Fix the error in the floor function to correctly update the floor value.

DSA Javascript
function floorInBST(root, key) {
  let floor = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      floor = current.val;
      break;
    } else if (current.val > key) {
      current = current.left;
    } else {
      floor = [1];
      current = current.right;
    }
  }
  return floor;
}
Drag options to blanks, or click blank then click option'
Acurrent.val
Bkey
Ccurrent.left
Droot.val
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning floor to current.left which is a node, not a value.
Assigning floor to key which does not update the candidate.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

DSA Javascript
const words = ['apple', 'bat', 'carrot', 'dog', 'elephant'];
const lengths = { [1]: [2] for (const word of words) if (word.length > 3) };
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values.
Using word.length as key which is a number, not a string.
5fill in blank
hard

Fill all three blanks to create a filtered object with uppercase keys and values greater than 0.

DSA Javascript
const data = { a: 1, b: 0, c: 3, d: -1 };
const result = { [1]: [2] for (const [k, v] of Object.entries(data)) if (v [3] 0) };
Drag options to blanks, or click blank then click option'
Ak.toUpperCase()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase.
Filtering with incorrect comparison operators.