0
0
DSA Typescriptprogramming~10 mins

Floor and Ceil in BST in DSA Typescript - 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 Typescript
function floorInBST(root: TreeNode | null, key: number): number | null {
  let floor: number | null = null;
  let current = [1];
  while (current !== null) {
    if (current.val === key) {
      return current.val;
    } 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'
Anull
Bkey
Croot
Dfloor
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from null instead of root.
Using the key as the starting node.
2fill in blank
medium

Complete the code to update the ceil value while searching in the BST.

DSA Typescript
function ceilInBST(root: TreeNode | null, key: number): number | null {
  let ceil: number | null = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      return current.val;
    } else if (current.val < key) {
      current = current.right;
    } else {
      [1] = current.val;
      current = current.left;
    }
  }
  return ceil;
}
Drag options to blanks, or click blank then click option'
Aceil
Bfloor
Ckey
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Updating floor instead of ceil.
Not updating ceil at all.
3fill in blank
hard

Fix the error in the floor function to correctly return null when no floor exists.

DSA Typescript
function floorInBST(root: TreeNode | null, key: number): number | null {
  let floor: number | null = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      return current.val;
    } else if (current.val > key) {
      current = current.left;
    } else {
      floor = current.val;
      current = current.right;
    }
  }
  return [1];
}
Drag options to blanks, or click blank then click option'
Afloor
Bkey
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or -1 which may not be correct floor.
Returning key which is unrelated.
4fill in blank
hard

Fill both blanks to complete the ceil function that returns null if no ceil exists.

DSA Typescript
function ceilInBST(root: TreeNode | null, key: number): number | null {
  let ceil: number | null = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      return current.val;
    } else if (current.val < key) {
      current = current.[1];
    } else {
      ceil = current.val;
      current = current.[2];
    }
  }
  return ceil;
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers.
Using parent or root instead of child pointers.
5fill in blank
hard

Fill both blanks to create a function that returns both floor and ceil in one pass.

DSA Typescript
function floorAndCeilInBST(root: TreeNode | null, key: number): { floor: number | null; ceil: number | null } {
  let floor: number | null = null;
  let ceil: number | null = null;
  let current = root;
  while (current !== null) {
    if (current.val === key) {
      floor = current.val;
      ceil = current.val;
      break;
    } else if (current.val < key) {
      floor = current.val;
      current = current.[1];
    } else {
      ceil = current.val;
      current = current.[2];
    }
  }
  return { floor: floor, ceil: ceil };
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers.
Adding extra values in return statement.