Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

BST Find Minimum Element in DSA Javascript - 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 code that finds the minimum element in a Binary Search Tree (BST)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root) {
  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));
A5
B7
C2
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
❓ Predict Output
intermediate
2:00remaining
Find minimum in BST with single node
What will the output be when finding the minimum element in a BST with only one node?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root) {
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(42);

console.log(findMin(root));
A42
Bnull
Cundefined
DError
Attempts:
2 left
💡 Hint
If there is only one node, that node is the minimum.
❓ Predict Output
advanced
2:00remaining
Find minimum element in BST with null root
What error or output will this code produce when trying to find the minimum element in an empty BST (root is null)?
DSA Javascript
function findMin(root) {
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = null;

console.log(findMin(root));
Anull
BTypeError: Cannot read property 'left' of null
Cundefined
D0
Attempts:
2 left
💡 Hint
Check what happens when root is null and you try to access root.left.
🧠 Conceptual
advanced
2:00remaining
Why does findMin in BST only check left children?
Why does the function to find the minimum element in a BST only traverse left children and not right children?
ABecause right children are always larger, so checking them is unnecessary for minimum.
BBecause the minimum could be anywhere, but left is checked first by convention.
CBecause the function is incorrect and should check both sides.
DBecause in a BST, all left children are smaller than their parent nodes, so the minimum is found by going left.
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children.
❓ Predict Output
expert
2:00remaining
Find minimum element after BST modification
Given the BST below, what is the output after inserting a new node with value 1 and then finding the minimum element?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function insert(root, value) {
  if (root === null) return new Node(value);
  if (value < root.value) {
    root.left = insert(root.left, value);
  } else {
    root.right = insert(root.right, value);
  }
  return root;
}

function findMin(root) {
  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);

insert(root, 1);

console.log(findMin(root));
A1
B2
C5
D10
Attempts:
2 left
💡 Hint
After inserting 1, it becomes the new leftmost node.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At the rightmost node
B. At the root node
C. At the leftmost node
D. At any leaf 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 as far left as possible.
  3. Final Answer:

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

    Minimum = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
  • Thinking minimum is at root
  • Confusing leftmost with rightmost
  • Assuming minimum can be any leaf
2. Which of the following JavaScript code snippets correctly finds the minimum element in a BST node structure where each node has left and value properties?
easy
A. function findMin(node) { while(node.left) { node = node.left; } return node.value; }
B. function findMin(node) { while(node.right) { node = node.right; } return node.value; }
C. function findMin(node) { return node.value; }
D. function findMin(node) { if(node.right) return findMin(node.right); else return node.value; }

Solution

  1. Step 1: Check traversal direction

    Minimum is found by moving left, so loop should check node.left.
  2. Step 2: Verify code correctness

    function findMin(node) { while(node.left) { node = node.left; } return node.value; } loops while node.left exists, then returns node.value, which is correct.
  3. Final Answer:

    function findMin(node) { while(node.left) { node = node.left; } return node.value; } -> Option A
  4. Quick Check:

    Traverse left until no left child [OK]
Hint: Loop left child nodes to find minimum [OK]
Common Mistakes:
  • Looping right child instead of left
  • Returning node.value without traversal
  • Recursing on right child for minimum
3. Given the BST below and the function findMin that finds the minimum value, what will be the output?
const root = {
  value: 10,
  left: { value: 5, left: { value: 2, left: null, right: null }, right: null },
  right: { value: 15, left: null, right: null }
};

function findMin(node) {
  while(node.left) {
    node = node.left;
  }
  return node.value;
}

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

Solution

  1. Step 1: Trace left children from root

    Start at 10, move left to 5, then left to 2, which has no left child.
  2. Step 2: Return value of leftmost node

    Leftmost node is 2, so function returns 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Leftmost node value = 2 [OK]
Hint: Minimum is leftmost node value 2 here [OK]
Common Mistakes:
  • Returning root value instead of leftmost
  • Confusing left and right child traversal
  • Misreading tree structure
4. The following code is intended to find the minimum value in a BST but has a bug. What is the bug?
function findMin(node) {
  while(node.right) {
    node = node.right;
  }
  return node.value;
}
medium
A. It incorrectly traverses right child instead of left
B. It does not return the node value
C. It causes infinite loop
D. It throws syntax error

Solution

  1. Step 1: Analyze traversal direction

    The code moves to node.right repeatedly, which finds the maximum, not minimum.
  2. Step 2: Identify correct traversal for minimum

    Minimum requires moving to node.left, so the bug is wrong direction.
  3. Final Answer:

    It incorrectly traverses right child instead of left -> Option A
  4. Quick Check:

    Minimum = leftmost, not rightmost [OK]
Hint: Minimum is leftmost, not rightmost traversal [OK]
Common Mistakes:
  • Confusing left and right child traversal
  • Assuming right child leads to minimum
  • Not testing code with example trees
5. You want to find the minimum element in a BST that may be empty (null). Which of the following improved functions correctly handles this edge case and returns null if the tree is empty?
hard
A. function findMin(node) { while(node.left) { node = node.left; } return node.value; }
B. function findMin(node) { if(!node) return null; while(node.left) { node = node.left; } return node.value; }
C. function findMin(node) { if(node.left) return findMin(node.left); else return node.value; }
D. function findMin(node) { if(!node.left) return null; else return findMin(node.left); }

Solution

  1. Step 1: Check for empty tree

    function findMin(node) { if(!node) return null; while(node.left) { node = node.left; } return node.value; } returns null immediately if node is null, handling empty tree.
  2. Step 2: Traverse left children safely

    Then it loops left until no left child, returning minimum value correctly.
  3. Final Answer:

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

    Check null first, then traverse left [OK]
Hint: Check for null before traversal to avoid errors [OK]
Common Mistakes:
  • Not checking for null input
  • Returning null incorrectly when node exists
  • Recursing without base case for null
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