0
0
DSA Javascriptprogramming~20 mins

BST Find Maximum Element in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Maximum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find maximum element in a BST - Output after insertions
Given the following JavaScript code that inserts elements into a Binary Search Tree (BST) and finds the maximum element, what is the printed output?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (data < current.data) {
        if (!current.left) {
          current.left = newNode;
          break;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          break;
        }
        current = current.right;
      }
    }
  }

  findMax() {
    if (!this.root) return null;
    let current = this.root;
    while (current.right) {
      current = current.right;
    }
    return current.data;
  }
}

const bst = new BST();
bst.insert(15);
bst.insert(10);
bst.insert(20);
bst.insert(8);
bst.insert(12);
bst.insert(17);
bst.insert(25);
console.log(bst.findMax());
A17
B20
C15
D25
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
Predict Output
intermediate
1:30remaining
Output of findMax on empty BST
What is the output of the following code when findMax is called on an empty BST?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  findMax() {
    if (!this.root) return null;
    let current = this.root;
    while (current.right) {
      current = current.right;
    }
    return current.data;
  }
}

const bst = new BST();
console.log(bst.findMax());
Anull
Bundefined
C0
DThrows an error
Attempts:
2 left
💡 Hint
Check what findMax returns if root is null.
🔧 Debug
advanced
2:00remaining
Identify the error in findMax method
What error will the following code produce when findMax is called on a BST with nodes?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  findMax() {
    let current = this.root;
    while (current.right !== null) {
      current = current.right;
    }
    return current.data;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(20);
console.log(bst.findMax());
ATypeError: Cannot read property 'right' of null
BReferenceError: bst.insert is not a function
COutput: 20
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check if insert method is defined in the BST class.
Predict Output
advanced
2:30remaining
Output after removing maximum element
Given the BST and the removeMax method below, what is the output of findMax after removing the maximum element?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (data < current.data) {
        if (!current.left) {
          current.left = newNode;
          break;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          break;
        }
        current = current.right;
      }
    }
  }

  findMax() {
    if (!this.root) return null;
    let current = this.root;
    while (current.right) {
      current = current.right;
    }
    return current.data;
  }

  removeMax() {
    if (!this.root) return null;
    let parent = null;
    let current = this.root;
    while (current.right) {
      parent = current;
      current = current.right;
    }
    if (!parent) {
      this.root = current.left;
    } else {
      parent.right = current.left;
    }
    return current.data;
  }
}

const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(12);
bst.insert(20);
bst.removeMax();
console.log(bst.findMax());
A15
B20
C12
Dnull
Attempts:
2 left
💡 Hint
Removing max removes the rightmost node, so max changes to its parent or left child.
🧠 Conceptual
expert
1:30remaining
Why is findMax efficient in a BST?
Why does the findMax method in a Binary Search Tree run efficiently compared to searching maximum in an unsorted binary tree?
ABecause BST stores elements in a linked list fashion making maximum retrieval O(1).
BBecause BST uses hashing internally to find maximum quickly.
CBecause BST stores elements so that the maximum is always the rightmost node, allowing direct traversal without checking all nodes.
DBecause BST sorts elements after every insertion, so maximum is at the root.
Attempts:
2 left
💡 Hint
Think about the BST property and how it organizes data.