Bird
Raised Fist0
DSA Javaprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Maximum Finder 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 insertion
Given the following Java code that inserts elements into a Binary Search Tree (BST) and then finds the maximum element, what will be the printed output?
DSA Java
class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}

class BST {
    Node root;

    void insert(int data) {
        root = insertRec(root, data);
    }

    Node insertRec(Node root, int data) {
        if (root == null) {
            root = new Node(data);
            return root;
        }
        if (data < root.data) {
            root.left = insertRec(root.left, data);
        } else if (data > root.data) {
            root.right = insertRec(root.right, data);
        }
        return root;
    }

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

public class Main {
    public static void main(String[] args) {
        BST tree = new BST();
        tree.insert(15);
        tree.insert(10);
        tree.insert(20);
        tree.insert(8);
        tree.insert(12);
        tree.insert(25);
        System.out.println(tree.findMax());
    }
}
A20
B12
C15
D25
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
❓ Predict Output
intermediate
2:00remaining
What is the output when the BST is empty?
Consider the same BST class as before. What happens when findMax() is called on an empty BST (no nodes inserted)?
DSA Java
class BST {
    Node root;

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

public class Main {
    public static void main(String[] args) {
        BST tree = new BST();
        System.out.println(tree.findMax());
    }
}
ANullPointerException
B-1
C0
DCompilation error
Attempts:
2 left
💡 Hint
What happens if you try to access a property of a null object?
🧠 Conceptual
advanced
1:30remaining
Why does findMax() only traverse right children in a BST?
In a Binary Search Tree, why is it sufficient to traverse only the right children to find the maximum element?
ABecause the tree is balanced and right children are always leaf nodes
BBecause left children always have the maximum value
CBecause all right children have values greater than their parent nodes
DBecause the root node always contains the maximum value
Attempts:
2 left
💡 Hint
Recall the BST property about left and right children values.
🔧 Debug
advanced
2:00remaining
Identify the bug in this findMax() method
What is wrong with the following findMax() method in a BST class?
DSA Java
int findMax() {
    Node current = root;
    while (current.left != null) {
        current = current.left;
    }
    return current.data;
}
AAll of the above
BIt will cause a NullPointerException if the tree is empty
CIt returns the minimum element instead of maximum
DIt incorrectly traverses left children instead of right children
Attempts:
2 left
💡 Hint
Think about which side holds larger values in a BST.
🚀 Application
expert
2:30remaining
After inserting nodes, what is the maximum element in this BST?
You insert the following integers into an empty BST in this order: 30, 20, 40, 35, 50, 45. What is the maximum element in the BST after all insertions?
A45
B50
C40
D35
Attempts:
2 left
💡 Hint
The maximum is the rightmost node after all insertions.

Practice

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

Solution

  1. Step 1: Understand BST property

    In a BST, left child nodes are smaller and right child nodes are larger than the parent node.
  2. Step 2: Locate maximum element

    The maximum element is the rightmost node because it has no larger right child.
  3. Final Answer:

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

    Maximum = rightmost node [OK]
Hint: Maximum is always the farthest right node in BST [OK]
Common Mistakes:
  • Confusing maximum with root node
  • Thinking maximum is at leftmost node
  • Assuming maximum can be any leaf
2. Which of the following Java method signatures correctly finds the maximum element in a BST node class named Node?
easy
A. public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; }
B. public int findMax(Node root) { return root.left.data; }
C. public int findMax(Node root) { if(root == null) return 0; return root.data; }
D. public int findMax(Node root) { while(root.left != null) root = root.left; return root.data; }

Solution

  1. Step 1: Check traversal direction for maximum

    Maximum is found by moving right until no right child exists.
  2. Step 2: Verify method logic

    public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } correctly loops while root.right is not null, then returns root.data.
  3. Final Answer:

    public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } -> Option A
  4. Quick Check:

    Traverse right to find max [OK]
Hint: Loop right child until null to find max [OK]
Common Mistakes:
  • Looping left child instead of right
  • Returning root data without traversal
  • Not checking for null root
3. What will be the output of the following Java code snippet?
class Node {
  int data;
  Node left, right;
  Node(int d) { data = d; left = right = null; }
}

public class BST {
  public static int findMax(Node root) {
    while(root.right != null) root = root.right;
    return root.data;
  }

  public static void main(String[] args) {
    Node root = new Node(10);
    root.left = new Node(5);
    root.right = new Node(20);
    root.right.right = new Node(30);
    System.out.println(findMax(root));
  }
}
medium
A. 20
B. 30
C. 10
D. 5

Solution

  1. Step 1: Trace the BST structure

    Root=10, left=5, right=20, right.right=30. Maximum is rightmost node 30.
  2. Step 2: Follow findMax method

    Loop moves from 10 to 20 to 30, then stops as 30.right is null, returns 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Rightmost node data = 30 [OK]
Hint: Follow right children to last node for max [OK]
Common Mistakes:
  • Returning root data without traversal
  • Confusing left and right children
  • Ignoring right.right node
4. Identify the error in the following Java method to find the maximum element in a BST and select the corrected version.
public int findMax(Node root) {
  if(root == null) return -1;
  while(root.left != null) {
    root = root.left;
  }
  return root.data;
}
medium
A. Add recursive call instead of while loop
B. Return 0 instead of -1 when root is null
C. Change root.left to root.right in while loop
D. No error, code is correct

Solution

  1. Step 1: Analyze traversal direction

    Maximum is found by moving right, but code moves left incorrectly.
  2. Step 2: Correct the while loop

    Change while(root.left != null) to while(root.right != null) to find max correctly.
  3. Final Answer:

    Change root.left to root.right in while loop -> Option C
  4. Quick Check:

    Traverse right child for max [OK]
Hint: Max is rightmost, so loop on right child [OK]
Common Mistakes:
  • Looping left child instead of right
  • Returning wrong default value
  • Thinking recursion is mandatory
5. Given a BST where some nodes have duplicate values, which approach correctly finds the maximum element considering duplicates?
public int findMaxWithDuplicates(Node root) {
  if(root == null) return Integer.MIN_VALUE;
  int maxVal = root.data;
  while(root.right != null) {
    root = root.right;
    if(root.data > maxVal) maxVal = root.data;
  }
  return maxVal;
}
What is the output of findMaxWithDuplicates for this BST? Root=15, root.left=10, root.right=20, root.right.right=20, root.right.right.right=25
hard
A. 25
B. 20
C. 15
D. Duplicate values cause error

Solution

  1. Step 1: Understand BST with duplicates

    Duplicates can appear on right subtree; max is still rightmost largest value.
  2. Step 2: Trace method execution

    Traverse right: 15 -> 20 -> 20 -> 25; maxVal updates to 25 at last node.
  3. Final Answer:

    25 -> Option A
  4. Quick Check:

    Rightmost node data = 25 [OK]
Hint: Rightmost node holds max even with duplicates [OK]
Common Mistakes:
  • Stopping at first duplicate max value
  • Assuming duplicates cause errors
  • Ignoring rightmost traversal
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