0
0
DSA Typescriptprogramming~20 mins

Floor and Ceil in BST in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floor and Ceil Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Floor and Ceil for a given key in BST
What is the output of the floor and ceil values for key 15 in the given BST after running the code?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insert(root: Node | null, val: number): Node {
  if (!root) return new Node(val);
  if (val < root.val) root.left = insert(root.left, val);
  else root.right = insert(root.right, val);
  return root;
}

function floor(root: Node | null, key: number): number | null {
  let res: number | null = null;
  while (root) {
    if (root.val === key) return root.val;
    if (root.val > key) root = root.left;
    else {
      res = root.val;
      root = root.right;
    }
  }
  return res;
}

function ceil(root: Node | null, key: number): number | null {
  let res: number | null = null;
  while (root) {
    if (root.val === key) return root.val;
    if (root.val < key) root = root.right;
    else {
      res = root.val;
      root = root.left;
    }
  }
  return res;
}

const values = [10, 5, 18, 3, 7, 12, 20];
let root: Node | null = null;
for (const v of values) {
  root = insert(root, v);
}

const key = 15;
const floorVal = floor(root, key);
const ceilVal = ceil(root, key);
console.log(`Floor: ${floorVal}, Ceil: ${ceilVal}`);
AFloor: 12, Ceil: 18
BFloor: 10, Ceil: 12
CFloor: 15, Ceil: 15
DFloor: 7, Ceil: 20
Attempts:
2 left
💡 Hint
Trace the BST traversal for floor and ceil separately, comparing node values with the key.
🧠 Conceptual
intermediate
1:00remaining
Number of nodes visited in floor search
In the worst case, how many nodes does the floor function visit in a BST with n nodes?
AO(1) node
BO(log n) nodes
CO(n) nodes
DO(n log n) nodes
Attempts:
2 left
💡 Hint
Consider the shape of the BST in the worst case (like a linked list).
🔧 Debug
advanced
1:30remaining
Identify the error in ceil function implementation
What error will occur when running this ceil function on a BST if the key is larger than all nodes?
DSA Typescript
function ceil(root: Node | null, key: number): number {
  let res: number | null = null;
  while (root) {
    if (root.val === key) return root.val;
    if (root.val < key) root = root.right;
    else {
      res = root.val;
      root = root.left;
    }
  }
  return res!;
}
AReturns null without error
BThrows a runtime error due to null assertion
CReturns incorrect ceil value
DInfinite loop
Attempts:
2 left
💡 Hint
What happens if res remains null and you use the non-null assertion operator?
Predict Output
advanced
2:00remaining
Output after inserting nodes and finding floor and ceil
What is the output after inserting nodes [8, 4, 12, 2, 6, 10, 14] and finding floor and ceil for key 5?
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insert(root: Node | null, val: number): Node {
  if (!root) return new Node(val);
  if (val < root.val) root.left = insert(root.left, val);
  else root.right = insert(root.right, val);
  return root;
}

function floor(root: Node | null, key: number): number | null {
  let res: number | null = null;
  while (root) {
    if (root.val === key) return root.val;
    if (root.val > key) root = root.left;
    else {
      res = root.val;
      root = root.right;
    }
  }
  return res;
}

function ceil(root: Node | null, key: number): number | null {
  let res: number | null = null;
  while (root) {
    if (root.val === key) return root.val;
    if (root.val < key) root = root.right;
    else {
      res = root.val;
      root = root.left;
    }
  }
  return res;
}

const values = [8, 4, 12, 2, 6, 10, 14];
let root: Node | null = null;
for (const v of values) {
  root = insert(root, v);
}

const key = 5;
const floorVal = floor(root, key);
const ceilVal = ceil(root, key);
console.log(`Floor: ${floorVal}, Ceil: ${ceilVal}`);
AFloor: 2, Ceil: 4
BFloor: 6, Ceil: 8
CFloor: 5, Ceil: 5
DFloor: 4, Ceil: 6
Attempts:
2 left
💡 Hint
Check nodes less than and greater than 5 in the BST.
🧠 Conceptual
expert
1:30remaining
Effect of BST shape on floor and ceil efficiency
How does the shape of a BST affect the efficiency of floor and ceil operations?
ABalanced BST gives O(log n) time; skewed BST gives O(n) time
BShape does not affect efficiency; always O(log n)
CSkewed BST gives O(1) time; balanced BST gives O(n) time
DEfficiency depends only on the key value, not BST shape
Attempts:
2 left
💡 Hint
Consider the height of the tree and path length for search.