Bird
Raised Fist0
DSA C#programming~20 mins

Floor and Ceil in BST in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST 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 Value in BST
What is the floor value of 15 in the given BST after inserting the nodes 10, 5, 20, 12, 30?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

class BST {
    public Node root;
    public void Insert(int val) {
        root = InsertRec(root, val);
    }
    private Node InsertRec(Node root, int val) {
        if (root == null) return new Node(val);
        if (val < root.val) root.left = InsertRec(root.left, val);
        else root.right = InsertRec(root.right, val);
        return root;
    }
    public int Floor(int key) {
        Node res = null;
        Node cur = root;
        while (cur != null) {
            if (cur.val == key) return cur.val;
            if (cur.val > key) cur = cur.left;
            else {
                res = cur;
                cur = cur.right;
            }
        }
        return res == null ? -1 : res.val;
    }
}

var bst = new BST();
bst.Insert(10);
bst.Insert(5);
bst.Insert(20);
bst.Insert(12);
bst.Insert(30);
int floorValue = bst.Floor(15);
System.Console.WriteLine(floorValue);
A20
B12
C15
D10
Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key in BST.
❓ Predict Output
intermediate
2:00remaining
Find Ceil Value in BST
What is the ceil value of 13 in the BST after inserting nodes 8, 4, 10, 14, 20?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

class BST {
    public Node root;
    public void Insert(int val) {
        root = InsertRec(root, val);
    }
    private Node InsertRec(Node root, int val) {
        if (root == null) return new Node(val);
        if (val < root.val) root.left = InsertRec(root.left, val);
        else root.right = InsertRec(root.right, val);
        return root;
    }
    public int Ceil(int key) {
        Node res = null;
        Node cur = root;
        while (cur != null) {
            if (cur.val == key) return cur.val;
            if (cur.val < key) cur = cur.right;
            else {
                res = cur;
                cur = cur.left;
            }
        }
        return res == null ? -1 : res.val;
    }
}

var bst = new BST();
bst.Insert(8);
bst.Insert(4);
bst.Insert(10);
bst.Insert(14);
bst.Insert(20);
int ceilValue = bst.Ceil(13);
System.Console.WriteLine(ceilValue);
A14
B20
C10
D13
Attempts:
2 left
💡 Hint
Ceil is the smallest value greater than or equal to the key in BST.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Floor Function
What error will occur when running this Floor function code on a BST?
DSA C#
public int Floor(Node root, int key) {
    if (root == null) return -1;
    if (root.val == key) return root.val;
    if (root.val > key) return Floor(root.left, key);
    int floorValue = Floor(root.right, key);
    return floorValue >= key ? floorValue : root.val;
}
ACompilation error due to missing return statement
BStackOverflowError due to infinite recursion
CReturns incorrect floor value for some keys
DAlways returns -1 regardless of input
Attempts:
2 left
💡 Hint
Check the condition comparing floorValue and key before returning.
❓ Predict Output
advanced
2:00remaining
Floor and Ceil for Non-Existing Key
Given a BST with nodes 7, 3, 9, 1, 5, what are the floor and ceil values for key 6?
DSA C#
class Node {
    public int val;
    public Node left, right;
    public Node(int v) { val = v; left = right = null; }
}

class BST {
    public Node root;
    public void Insert(int val) {
        root = InsertRec(root, val);
    }
    private Node InsertRec(Node root, int val) {
        if (root == null) return new Node(val);
        if (val < root.val) root.left = InsertRec(root.left, val);
        else root.right = InsertRec(root.right, val);
        return root;
    }
    public int Floor(int key) {
        Node res = null;
        Node cur = root;
        while (cur != null) {
            if (cur.val == key) return cur.val;
            if (cur.val > key) cur = cur.left;
            else {
                res = cur;
                cur = cur.right;
            }
        }
        return res == null ? -1 : res.val;
    }
    public int Ceil(int key) {
        Node res = null;
        Node cur = root;
        while (cur != null) {
            if (cur.val == key) return cur.val;
            if (cur.val < key) cur = cur.right;
            else {
                res = cur;
                cur = cur.left;
            }
        }
        return res == null ? -1 : res.val;
    }
}

var bst = new BST();
bst.Insert(7);
bst.Insert(3);
bst.Insert(9);
bst.Insert(1);
bst.Insert(5);
int floorValue = bst.Floor(6);
int ceilValue = bst.Ceil(6);
System.Console.WriteLine($"Floor: {floorValue}, Ceil: {ceilValue}");
AFloor: -1, Ceil: -1
BFloor: 6, Ceil: 6
CFloor: 7, Ceil: 9
DFloor: 5, Ceil: 7
Attempts:
2 left
💡 Hint
Floor is greatest smaller or equal, ceil is smallest greater or equal.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Floor and Ceil in BST
What is the worst-case time complexity of finding floor or ceil in a BST with n nodes?
AO(n) when BST is skewed, O(log n) when balanced
BAlways O(log n) regardless of BST shape
CAlways O(n^2) due to recursive calls
DO(1) because BST allows direct access
Attempts:
2 left
💡 Hint
Consider BST shape impact on search path length.

Practice

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

Solution

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

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

    The greatest value in the BST less than or equal to the given value matches this definition exactly, while others describe ceil or unrelated concepts.
  3. Final Answer:

    The greatest value in the BST less than or equal to the given value -> Option C
  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 always smaller than the value
  • Assuming floor is the root value
2. Which of the following is the correct C# method signature to find the ceil value in a BST node class?
easy
A. public int FindCeil(int key)
B. int FindCeil(int key, Node root)
C. public void FindCeil(Node root, int key)
D. public int FindCeil(Node root, int key)

Solution

  1. Step 1: Understand method signature requirements

    The method should be public, return an int (the ceil value), and accept the BST root node and the key to find ceil for.
  2. Step 2: Match with options

    public int FindCeil(Node root, int key) matches this: public int FindCeil(Node root, int key). int FindCeil(int key, Node root) has wrong parameter order, C returns void, D misses root parameter.
  3. Final Answer:

    public int FindCeil(Node root, int key) -> Option D
  4. Quick Check:

    Correct signature returns int and takes root, key [OK]
Hint: Method returns int, takes root and key in order [OK]
Common Mistakes:
  • Wrong parameter order
  • Using void return type
  • Missing root parameter
3. Given the BST below and key = 14, what is the ceil value?

        10
       /  \
      5    15
          /  \
         12   20
medium
A. 12
B. 15
C. 20
D. 10

Solution

  1. Step 1: Understand ceil definition and BST structure

    Ceil is the smallest value greater than or equal to 14. The BST nodes are 10, 5, 15, 12, 20.
  2. Step 2: Find values ≥ 14 and pick smallest

    Values ≥ 14 are 15 and 20. The smallest among these is 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    Ceil(14) = 15 [OK]
Hint: Ceil is smallest ≥ key in BST [OK]
Common Mistakes:
  • Choosing 12 which is less than 14
  • Choosing 20 which is greater but not smallest
  • Confusing ceil with floor
4. Consider this buggy C# code snippet to find floor in BST:

int FindFloor(Node root, int key) {
  if (root == null) return -1;
  if (root.val == key) return root.val;
  if (root.val > key) return FindFloor(root.left, key);
  int floorVal = FindFloor(root.right, key);
  return (floorVal <= key && floorVal != -1) ? floorVal : root.val;
}
What is the main issue causing incorrect floor results?
medium
A. The condition to check floorVal is incorrect and may return wrong value
B. The base case returns -1 which may be confused with valid floor
C. The function does not handle the case when root.val < key
D. The function does not recurse on left subtree when root.val < key

Solution

  1. Step 1: Analyze the floorVal condition

    The code checks if floorVal ≤ key and floorVal != -1 to decide return value. But floorVal could be less than root.val and still valid floor.
  2. Step 2: Identify the incorrect logic

    The condition should check if floorVal is greater than root.val to pick the better floor, but it doesn't, causing wrong floor return.
  3. Final Answer:

    The condition to check floorVal is incorrect and may return wrong value -> Option A
  4. Quick Check:

    Incorrect floorVal check causes wrong floor [OK]
Hint: Check floorVal against root.val, not just key [OK]
Common Mistakes:
  • Ignoring comparison with root.val
  • Returning -1 as valid floor
  • Not handling null root properly
5. You want to find both floor and ceil of a key in a BST efficiently. Which approach is best to implement in C#?
hard
A. Traverse the BST once, updating floor and ceil candidates during traversal
B. Perform two separate traversals: one for floor and one for ceil
C. Convert BST to sorted array, then use binary search twice
D. Use BFS traversal to find floor and ceil simultaneously

Solution

  1. Step 1: Consider efficiency of approaches

    Two separate traversals or converting to array add extra time or space. BFS is not suitable for ordered search.
  2. Step 2: Single traversal updating floor and ceil

    During one BST traversal, compare node values with key to update floor (largest ≤ key) and ceil (smallest ≥ key) efficiently.
  3. Final Answer:

    Traverse the BST once, updating floor and ceil candidates during traversal -> Option A
  4. Quick Check:

    Single traversal updates floor and ceil efficiently [OK]
Hint: Update floor and ceil in one BST traversal [OK]
Common Mistakes:
  • Doing two separate traversals wasting time
  • Using BFS which ignores BST order
  • Converting BST to array wastes space
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