Bird
Raised Fist0
DSA C#programming~20 mins

BST Find Maximum Element in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Max Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find the maximum element in a BST
What is the output of the following C# code that finds the maximum element in a Binary Search Tree (BST)?
DSA C#
using System;

class Node {
    public int data;
    public Node left, right;
    public Node(int item) {
        data = item;
        left = right = null;
    }
}

class BST {
    public Node root;

    public int FindMax(Node node) {
        if (node == null) return int.MinValue;
        while (node.right != null) {
            node = node.right;
        }
        return node.data;
    }
}

class Program {
    static void Main() {
        BST tree = new BST();
        tree.root = new Node(10);
        tree.root.left = new Node(5);
        tree.root.right = new Node(20);
        tree.root.right.left = new Node(15);
        tree.root.right.right = new Node(25);

        Console.WriteLine(tree.FindMax(tree.root));
    }
}
A25
B10
C15
D20
Attempts:
2 left
💡 Hint
In a BST, the maximum element is the rightmost node.
❓ Predict Output
intermediate
2:00remaining
Output when BST is empty
What will be the output of the following code when the BST is empty (root is null)?
DSA C#
using System;

class Node {
    public int data;
    public Node left, right;
    public Node(int item) {
        data = item;
        left = right = null;
    }
}

class BST {
    public Node root;

    public int FindMax(Node node) {
        if (node == null) return int.MinValue;
        while (node.right != null) {
            node = node.right;
        }
        return node.data;
    }
}

class Program {
    static void Main() {
        BST tree = new BST();
        Console.WriteLine(tree.FindMax(tree.root));
    }
}
AThrows NullReferenceException
B0
CNo output (program crashes silently)
D-2147483648
Attempts:
2 left
💡 Hint
Check the return value when the input node is null.
🔧 Debug
advanced
2:00remaining
Identify the error in FindMax method
What error will the following code produce when trying to find the maximum element in a BST?
DSA C#
public int FindMax(Node node) {
    while (node.right != null) {
        node = node.left;
    }
    return node.data;
}
AReturns minimum element instead of maximum
BThrows NullReferenceException
CInfinite loop
DReturns maximum element correctly
Attempts:
2 left
💡 Hint
Check which child node is used inside the loop.
❓ Predict Output
advanced
2:00remaining
Maximum element after inserting nodes
After inserting the following nodes into an empty BST in this order: 30, 10, 40, 35, 50, what is the maximum element found by FindMax?
DSA C#
using System;

class Node {
    public int data;
    public Node left, right;
    public Node(int item) {
        data = item;
        left = right = null;
    }
}

class BST {
    public Node root;

    public void Insert(int key) {
        root = InsertRec(root, key);
    }

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

    public int FindMax(Node node) {
        if (node == null) return int.MinValue;
        while (node.right != null) {
            node = node.right;
        }
        return node.data;
    }
}

class Program {
    static void Main() {
        BST tree = new BST();
        tree.Insert(30);
        tree.Insert(10);
        tree.Insert(40);
        tree.Insert(35);
        tree.Insert(50);
        Console.WriteLine(tree.FindMax(tree.root));
    }
}
A35
B40
C50
D30
Attempts:
2 left
💡 Hint
The maximum element is the rightmost node after all insertions.
🧠 Conceptual
expert
2:00remaining
Why is FindMax efficient in a BST?
Why does the FindMax method in a BST run efficiently compared to searching the entire tree?
ABecause it uses recursion to check all nodes in the tree.
BBecause it only traverses the right children, which leads directly to the maximum element.
CBecause it balances the tree before searching.
DBecause it traverses both left and right children equally.
Attempts:
2 left
💡 Hint
Think about the BST property and where the maximum element is located.

Practice

(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At the root node
B. At the rightmost 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 and right child nodes have larger values compared to their parent.
  2. Step 2: Locate maximum element

    The maximum element is the largest value, so it must be the rightmost node because values increase to the right.
  3. Final Answer:

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

    Maximum element = rightmost node [OK]
Hint: Maximum is always the rightmost node in BST [OK]
Common Mistakes:
  • Thinking maximum is at the root
  • Confusing leftmost with maximum
  • Assuming maximum can be any leaf
2. Which of the following C# code snippets correctly finds the maximum element in a BST node called root?
easy
A. while (root.Left != null) { root = root.Left; } return root.Value;
B. if (root.Left == null) return root.Value; else return root.Left.Value;
C. while (root.Right != null) { root = root.Right; } return root.Value;
D. return root.Value; // root is always max

Solution

  1. Step 1: Check traversal direction

    To find max, traverse right child nodes until no more right child exists.
  2. Step 2: Verify code correctness

    while (root.Right != null) { root = root.Right; } return root.Value; uses a while loop to move right until root.Right is null, then returns root.Value, which is correct.
  3. Final Answer:

    while (root.Right != null) { root = root.Right; } return root.Value; -> Option C
  4. Quick Check:

    Traverse right until null, then return value [OK]
Hint: Traverse right child until null to find max [OK]
Common Mistakes:
  • Traversing left child instead of right
  • Returning root value without traversal
  • Using incorrect condition in loop
3. What will be the output of the following C# code snippet that finds the maximum element in a BST?
class Node {
  public int Value;
  public Node Left, Right;
  public Node(int val) { Value = val; Left = Right = null; }
}

Node root = new Node(10);
root.Left = new Node(5);
root.Right = new Node(15);
root.Right.Right = new Node(20);

Node current = root;
while (current.Right != null) {
  current = current.Right;
}
Console.WriteLine(current.Value);
medium
A. 5
B. 15
C. 10
D. 20

Solution

  1. Step 1: Trace the BST structure

    Root is 10, right child 15, right child of 15 is 20. So rightmost node is 20.
  2. Step 2: Follow the loop

    The loop moves current from 10 to 15, then to 20, then stops because 20.Right is null.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Rightmost node value = 20 [OK]
Hint: Rightmost node value is maximum [OK]
Common Mistakes:
  • Stopping at 15 instead of 20
  • Printing root value instead of max
  • Confusing left and right children
4. The following C# method is intended to find the maximum element in a BST. What is the error?
int FindMax(Node root) {
  if (root == null) return int.MinValue;
  if (root.Right == null) return root.Value;
  return FindMax(root.Left);
}
medium
A. It should recurse on root.Right, not root.Left
B. It should return int.MaxValue when root is null
C. It should check root.Left instead of root.Right
D. No error, code is correct

Solution

  1. Step 1: Analyze recursion direction

    To find max, recursion must go right, but code recurses left incorrectly.
  2. Step 2: Identify correct recursion

    Change FindMax(root.Left) to FindMax(root.Right) to find max correctly.
  3. Final Answer:

    It should recurse on root.Right, not root.Left -> Option A
  4. Quick Check:

    Max found by right recursion [OK]
Hint: Max search recurses right child, not left [OK]
Common Mistakes:
  • Recursing left child instead of right
  • Returning wrong base case value
  • Ignoring null root check
5. Given a BST where some nodes have duplicate values, which approach correctly finds the maximum value considering duplicates?
int FindMaxWithDuplicates(Node root) {
  if (root == null) return int.MinValue;
  int rightMax = FindMaxWithDuplicates(root.Right);
  return Math.Max(root.Value, rightMax);
}
What is the output of FindMaxWithDuplicates on this BST? Root: 10 Left: 5 Right: 10 Right.Right: 15 Right.Right.Right: 15
hard
A. 15
B. 10
C. 5
D. Error due to duplicates

Solution

  1. Step 1: Understand duplicate handling

    The method compares current node value with max from right subtree, so duplicates do not affect max finding.
  2. Step 2: Trace values

    Right subtree max is 15 (rightmost node). Root value 10 is less than 15, so max is 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Max value with duplicates = 15 [OK]
Hint: Compare node value with right subtree max for duplicates [OK]
Common Mistakes:
  • Assuming duplicates cause errors
  • Returning first max found without comparison
  • Ignoring right subtree values
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