Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

BST Find Minimum Element in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find minimum element in a BST
What is the output of the following TypeScript code that finds the minimum element in a Binary Search Tree (BST)?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(2);
root.left.right = new Node(7);
root.right.right = new Node(20);

console.log(findMin(root));
A2
B5
C7
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
❓ Predict Output
intermediate
2:00remaining
Minimum element when BST has only right children
What will the output be when finding the minimum element in a BST where all nodes only have right children?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(3);
root.right = new Node(5);
root.right.right = new Node(7);
root.right.right.right = new Node(10);

console.log(findMin(root));
A7
B5
C3
D10
Attempts:
2 left
💡 Hint
If there are no left children, the root is the minimum.
🔧 Debug
advanced
2:00remaining
Identify the error in this BST minimum finder
What error will this TypeScript code produce when trying to find the minimum element in a BST?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left) {
    current = current.right;
  }
  return current.value;
}

const root = new Node(8);
root.left = new Node(3);
root.left.left = new Node(1);

console.log(findMin(root));
ATypeError: Cannot read property 'left' of null
B8
C1
D10
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the while loop.
🧠 Conceptual
advanced
2:00remaining
Why does finding minimum in BST take O(h) time?
Why does finding the minimum element in a Binary Search Tree take time proportional to the height (h) of the tree?
ABecause the minimum element is always the root node, so it takes constant time.
BBecause the minimum is always found by traversing the left children from the root down to the leaf, which can be up to height h.
CBecause the minimum element is found by traversing all nodes in the tree, which takes O(n) time.
DBecause the minimum element is always the rightmost leaf, so traversal goes right h times.
Attempts:
2 left
💡 Hint
Think about how you move from root to the smallest value in a BST.
❓ Predict Output
expert
2:00remaining
Output of findMin on a BST with duplicate values
What is the output of the following code that finds the minimum element in a BST containing duplicate values?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(10);
root.left = new Node(10);
root.left.left = new Node(5);
root.left.left.left = new Node(5);
root.right = new Node(15);

console.log(findMin(root));
A15
B10
Cnull
D5
Attempts:
2 left
💡 Hint
Duplicates can appear on the left side; minimum is still the leftmost node.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At any leaf node
B. At the rightmost node
C. At the root node
D. At the leftmost node

Solution

  1. Step 1: Understand BST property

    In a BST, left child nodes have smaller values than their parents.
  2. Step 2: Locate minimum element

    The smallest value is found by going left until no more left child exists.
  3. Final Answer:

    At the leftmost node -> Option D
  4. Quick Check:

    Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Confusing minimum with root node
  • Thinking minimum is at rightmost node
  • Assuming minimum can be any leaf
2. Which TypeScript code snippet correctly finds the minimum element in a BST node?
easy
A. while (node.right !== null) { node = node.right; } return node.value;
B. while (node.left !== null) { node = node.left; } return node.value;
C. if (node.value < min) min = node.value; return min;
D. return Math.min(node.value, node.left.value);

Solution

  1. Step 1: Check traversal direction for minimum

    Minimum is found by moving left until no left child exists.
  2. Step 2: Verify code correctness

    while (node.left !== null) { node = node.left; } return node.value; correctly loops left and returns node value; others are incorrect or incomplete.
  3. Final Answer:

    while (node.left !== null) { node = node.left; } return node.value; -> Option B
  4. Quick Check:

    Traverse left to find minimum [OK]
Hint: Traverse left child nodes until null to find minimum [OK]
Common Mistakes:
  • Traversing right child instead of left
  • Using Math.min without checking null nodes
  • Not looping until leftmost node
3. What will be the output of the following TypeScript code?
class Node {
  constructor(public value: number, public left: Node | null = null, public right: Node | null = null) {}
}

const root = new Node(10, new Node(5, new Node(2), new Node(7)), new Node(15));

function findMin(node: Node): number {
  while (node.left !== null) {
    node = node.left;
  }
  return node.value;
}

console.log(findMin(root));
medium
A. 7
B. 5
C. 2
D. 10

Solution

  1. Step 1: Analyze BST structure

    Root is 10, left child 5, left-left child 2, left-right child 7, right child 15.
  2. Step 2: Trace findMin function

    Function moves left from 10 to 5, then to 2, which has no left child, so returns 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Minimum value in BST = 2 [OK]
Hint: Minimum is leftmost node value, here 2 [OK]
Common Mistakes:
  • Returning root value instead of leftmost
  • Confusing left and right child nodes
  • Stopping traversal too early
4. Identify the bug in this TypeScript function to find the minimum element in a BST:
function findMin(node: Node | null): number {
  if (node === null) return -1;
  while (node.right !== null) {
    node = node.right;
  }
  return node.value;
}
medium
A. Traverses right child instead of left child
B. Returns -1 for empty tree, should throw error
C. Does not check if node is null before accessing value
D. Returns node.value before loop ends

Solution

  1. Step 1: Check traversal direction

    Minimum element requires moving left, but code moves right.
  2. Step 2: Identify correct fix

    Change 'node.right' to 'node.left' in the while loop to find minimum.
  3. Final Answer:

    Traverses right child instead of left child -> Option A
  4. Quick Check:

    Minimum found by left traversal, not right [OK]
Hint: Minimum is leftmost, so traverse left child nodes [OK]
Common Mistakes:
  • Using right child traversal for minimum
  • Ignoring null checks before access
  • Returning default -1 instead of handling empty tree
5. Given a BST where some nodes have duplicate values, how would you modify the findMin function to return the smallest unique value?
hard
A. Traverse entire tree and collect all values, then find minimum
B. Return the first leftmost node value without checking duplicates
C. Traverse leftmost nodes and skip duplicates by checking parent values
D. Use rightmost node value as minimum to avoid duplicates

Solution

  1. Step 1: Understand duplicates in BST

    Duplicates can appear on left or right; leftmost node may not be unique minimum.
  2. Step 2: Modify approach to find unique minimum

    Traverse entire tree to collect all values, then find the smallest unique value.
  3. Final Answer:

    Traverse entire tree and collect all values, then find minimum -> Option A
  4. Quick Check:

    Full traversal needed for unique minimum [OK]
Hint: Traverse all nodes to find unique minimum value [OK]
Common Mistakes:
  • Assuming leftmost node is always unique minimum
  • Ignoring duplicates in traversal
  • Using rightmost node as minimum
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