Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Floor and Ceil in BST in DSA Typescript - Interactive Practice

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

Practice

(1/5)
1. What does the floor of a value in a Binary Search Tree (BST) represent?
easy
A. The greatest value in the BST less than or equal to the given value
B. The smallest value in the BST greater than or equal to the given value
C. The root value of the BST
D. The average of all values in the BST

Solution

  1. Step 1: Understand the definition of floor in BST

    The floor of a value in BST is the greatest value that is less than or equal to the given value.
  2. Step 2: Compare with other options

    The smallest value in the BST greater than or equal to the given value describes ceil, not floor. Options C and D are unrelated to floor concept.
  3. Final Answer:

    The greatest value in the BST less than or equal to the given value -> Option A
  4. Quick Check:

    Floor = greatest ≤ value [OK]
Hint: Floor is largest value ≤ target in BST [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor is the smallest value
  • Assuming floor is always the root
2. Which of the following TypeScript function signatures correctly represents a function to find the ceil in a BST?
easy
A. function findCeil(key: number, root: TreeNode): number
B. function findCeil(root: TreeNode | null, key: number): number | null
C. function findCeil(root: TreeNode, key: string): number
D. function findCeil(root: TreeNode | null): number

Solution

  1. Step 1: Check parameter order and types

    Function should accept root node (possibly null) and a key number to find ceil for.
  2. Step 2: Validate return type

    Return type should be number or null if no ceil found. function findCeil(root: TreeNode | null, key: number): number | null matches this.
  3. Final Answer:

    function findCeil(root: TreeNode | null, key: number): number | null -> Option B
  4. Quick Check:

    Correct params and return type [OK]
Hint: Root first, key second, return number or null [OK]
Common Mistakes:
  • Swapping parameter order
  • Using wrong parameter types
  • Missing null in root type
3. Given the BST below and key = 14, what is the ceil value?
       10
      /  \
     5    15
         /  \
        12   20
medium
A. 12
B. 20
C. 15
D. null

Solution

  1. Step 1: Locate key 14 in BST or nearest nodes

    14 is not in BST. Check nodes greater than or equal to 14: 15 and 20.
  2. Step 2: Find smallest node ≥ 14

    Between 15 and 20, 15 is smaller and ≥ 14, so ceil is 15.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    Ceil = smallest ≥ 14 is 15 [OK]
Hint: Ceil is smallest value ≥ key in BST [OK]
Common Mistakes:
  • Choosing 12 which is less than 14
  • Choosing 20 which is larger but not smallest ≥ 14
  • Returning null when ceil exists
4. Consider this TypeScript function to find floor in BST:
function findFloor(root: TreeNode | null, key: number): number | null {
  if (!root) return null;
  if (root.val === key) return root.val;
  const floorRight = findFloor(root.right, key);
  return floorRight !== null ? floorRight : root.val;
}
What is the bug in this code?
medium
A. It returns root.val even if floorRight is null and root.val > key
B. It does not check for null root
C. It returns key instead of floor value
D. It incorrectly calls findFloor on root.left when root.val < key

Solution

  1. Step 1: Identify missing logic for root.val > key

    The code does not recurse into the left subtree when root.val > key. Instead, it always computes floorRight from the right subtree.
  2. Step 2: Trace buggy return

    When root.val > key and right subtree yields null for floorRight (common case), it returns root.val which exceeds key, violating floor definition.
  3. Final Answer:

    It returns root.val even if floorRight is null and root.val > key -> Option A
  4. Quick Check:

    Missing left recursion on root.val > key [OK]
Hint: Check return when right subtree floor is null [OK]
Common Mistakes:
  • Ignoring null checks
  • Confusing left and right subtree calls
  • Assuming root.val always valid floor
5. You have a BST with values [8, 4, 12, 2, 6, 10, 14]. You want to find the floor and ceil of 5. Which pair is correct?
medium
A. Floor = 2, Ceil = 8
B. Floor = 6, Ceil = 4
C. Floor = 5, Ceil = 5
D. Floor = 4, Ceil = 6

Solution

  1. Step 1: Identify floor of 5 in BST

    Floor is greatest value ≤ 5. Values ≤ 5 are 2 and 4. Greatest is 4.
  2. Step 2: Identify ceil of 5 in BST

    Ceil is smallest value ≥ 5. Values ≥ 5 are 6, 8, 10, 12, 14. Smallest is 6.
  3. Final Answer:

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

    Floor ≤ 5 = 4, Ceil ≥ 5 = 6 [OK]
Hint: Floor ≤ key, Ceil ≥ key; pick closest values [OK]
Common Mistakes:
  • Swapping floor and ceil values
  • Assuming floor or ceil equals key if not present
  • Choosing values not in BST
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