Bird
Raised Fist0
DSA Javaprogramming~20 mins

Kth Smallest Element in BST in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Kth Smallest Element Function Call
What is the output of the following Java code that finds the 3rd smallest element in the given BST?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int x) { val = x; }
}

public class Solution {
    int count = 0;
    int result = -1;

    public int kthSmallest(TreeNode root, int k) {
        inorder(root, k);
        return result;
    }

    private void inorder(TreeNode node, int k) {
        if (node == null) return;
        inorder(node.left, k);
        count++;
        if (count == k) {
            result = node.val;
            return;
        }
        inorder(node.right, k);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.right = new TreeNode(6);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(4);
        root.left.left.left = new TreeNode(1);

        Solution sol = new Solution();
        System.out.println(sol.kthSmallest(root, 3));
    }
}
A5
B2
C3
D4
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST visits nodes in ascending order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Inorder Traversal in BST
Why does inorder traversal help in finding the kth smallest element in a BST?
ABecause inorder traversal visits nodes in descending order of their values.
BBecause inorder traversal visits nodes in ascending order of their values.
CBecause inorder traversal visits nodes randomly.
DBecause inorder traversal visits only leaf nodes.
Attempts:
2 left
💡 Hint
Think about the BST property and how inorder traversal works.
🔧 Debug
advanced
2:00remaining
Analyze the Kth Smallest Element Code
Which of the following is true about this code for finding the kth smallest element in a BST?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int x) { val = x; }
}

public class Solution {
    int count = 0;
    int result = -1;

    public int kthSmallest(TreeNode root, int k) {
        inorder(root, k);
        return result;
    }

    private void inorder(TreeNode node, int k) {
        if (node == null) return;
        inorder(node.left, k);
        if (count == k) return;
        count++;
        result = node.val;
        inorder(node.right, k);
    }
}
AThe code returns the kth smallest element correctly.
BThe code causes a NullPointerException.
CThe code returns -1 always.
DThe code returns the largest element instead of kth smallest.
Attempts:
2 left
💡 Hint
Observe the order: recurse left, check count, increment and set if not, recurse right. Note how pruning works.
❓ Predict Output
advanced
2:00remaining
Output of Kth Smallest Element with Duplicate Values
What is the output of the following code when finding the 4th smallest element in a BST that contains duplicates?
DSA Java
class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int x) { val = x; }
}

public class Solution {
    int count = 0;
    int result = -1;

    public int kthSmallest(TreeNode root, int k) {
        inorder(root, k);
        return result;
    }

    private void inorder(TreeNode node, int k) {
        if (node == null) return;
        inorder(node.left, k);
        count++;
        if (count == k) {
            result = node.val;
            return;
        }
        inorder(node.right, k);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.right = new TreeNode(6);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.left.left.left = new TreeNode(1);

        Solution sol = new Solution();
        System.out.println(sol.kthSmallest(root, 4));
    }
}
A3
B5
C6
D4
Attempts:
2 left
💡 Hint
Duplicates are counted as separate nodes in inorder traversal.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Kth Smallest Element in BST
What is the average time complexity of finding the kth smallest element in a balanced BST using inorder traversal?
AO(k)
BO(log n)
CO(n)
DO(k log n)
Attempts:
2 left
💡 Hint
Inorder traversal stops once kth element is found.

Practice

(1/5)
1. What does the term kth smallest element in a BST mean?
easy
A. The element with the kth highest frequency in the BST
B. The element that appears in the kth position when BST elements are sorted in ascending order
C. The element at the kth level of the BST
D. The element with the kth largest value in the BST

Solution

  1. Step 1: Understand BST property

    A Binary Search Tree (BST) stores elements so that left child is smaller and right child is larger.
  2. Step 2: Define kth smallest element

    The kth smallest element is the one that appears in the sorted order at position k.
  3. Final Answer:

    The element that appears in the kth position when BST elements are sorted in ascending order -> Option B
  4. Quick Check:

    kth smallest = kth in ascending order [OK]
Hint: Think of inorder traversal to get sorted order [OK]
Common Mistakes:
  • Confusing kth smallest with kth largest
  • Thinking kth smallest means kth level node
  • Mixing frequency with element order
2. Which of the following Java method signatures correctly represents a function to find the kth smallest element in a BST?
easy
A. public int kthSmallest(TreeNode root, int k)
B. public void kthSmallest(TreeNode root, int k)
C. public TreeNode kthSmallest(int k, TreeNode root)
D. public int kthSmallest(int k)

Solution

  1. Step 1: Check method parameters and return type

    The method should accept the BST root and integer k, and return the kth smallest element as int.
  2. Step 2: Match signature with correct order

    public int kthSmallest(TreeNode root, int k) matches: returns int, parameters (TreeNode root, int k).
  3. Final Answer:

    public int kthSmallest(TreeNode root, int k) -> Option A
  4. Quick Check:

    Return type int and parameters (root, k) [OK]
Hint: Return the element value, input root and k [OK]
Common Mistakes:
  • Using void return type instead of int
  • Swapping parameter order incorrectly
  • Missing root parameter
3. What will be the output of the following code snippet to find the 3rd smallest element in the BST?
class TreeNode {
  int val;
  TreeNode left, right;
  TreeNode(int v) { val = v; }
}

public class Solution {
  int count = 0;
  int result = -1;
  public int kthSmallest(TreeNode root, int k) {
    inorder(root, k);
    return result;
  }
  private void inorder(TreeNode node, int k) {
    if (node == null) return;
    inorder(node.left, k);
    count++;
    if (count == k) {
      result = node.val;
      return;
    }
    inorder(node.right, k);
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(5);
    root.left = new TreeNode(3);
    root.right = new TreeNode(6);
    root.left.left = new TreeNode(2);
    root.left.right = new TreeNode(4);
    root.left.left.left = new TreeNode(1);
    Solution sol = new Solution();
    System.out.println(sol.kthSmallest(root, 3));
  }
}
medium
A. 5
B. 2
C. 4
D. 3

Solution

  1. Step 1: Perform inorder traversal to get sorted elements

    Inorder traversal order: 1, 2, 3, 4, 5, 6.
  2. Step 2: Identify the 3rd element in sorted order

    The 3rd smallest element is 3.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Sorted BST elements = [1,2,3,4,5,6], 3rd = 3 [OK]
Hint: Inorder traversal gives sorted order; pick kth element [OK]
Common Mistakes:
  • Confusing preorder or postorder with inorder
  • Off-by-one error in counting
  • Returning node instead of node value
4. Identify the bug in the following code that attempts to find the kth smallest element in a BST:
public int kthSmallest(TreeNode root, int k) {
  int count = 0;
  int result = -1;
  inorder(root, k, count, result);
  return result;
}

private void inorder(TreeNode node, int k, int count, int result) {
  if (node == null) return;
  inorder(node.left, k, count, result);
  count++;
  if (count == k) {
    result = node.val;
    return;
  }
  inorder(node.right, k, count, result);
}
medium
A. Primitive variables count and result are passed by value, so changes don't persist
B. The inorder traversal is missing the base case
C. The method should return void instead of int
D. The count variable should be reset to zero inside inorder

Solution

  1. Step 1: Understand Java parameter passing

    Primitive types like int are passed by value, so changes inside methods don't affect original variables.
  2. Step 2: Identify impact on count and result

    Since count and result are passed by value, their updates inside inorder do not persist outside, causing incorrect result.
  3. Final Answer:

    Primitive variables count and result are passed by value, so changes don't persist -> Option A
  4. Quick Check:

    Primitive int passed by value, no update outside method [OK]
Hint: Use class fields or wrappers to persist count and result [OK]
Common Mistakes:
  • Assuming primitives are passed by reference
  • Not using class variables or arrays for count/result
  • Ignoring base case in recursion
5. Given a BST where nodes can have duplicate values, how would you modify the standard kth smallest element algorithm to correctly find the kth smallest unique element?
hard
A. Count all nodes including duplicates normally during inorder traversal
B. Use preorder traversal instead of inorder traversal
C. Use inorder traversal and skip duplicates by comparing current node value with previous visited value
D. Store all values in a list and sort it to find kth unique element

Solution

  1. Step 1: Understand duplicates in BST

    Duplicates appear as repeated values; inorder traversal lists them in sorted order including duplicates.
  2. Step 2: Modify traversal to skip duplicates

    Track previous visited value; only increment count when current value differs to count unique elements.
  3. Step 3: Find kth unique element

    Increment count only for unique values; when count == k, return current node value.
  4. Final Answer:

    Use inorder traversal and skip duplicates by comparing current node value with previous visited value -> Option C
  5. Quick Check:

    Skip duplicates by comparing with previous value during inorder [OK]
Hint: Track previous value to count unique elements only [OK]
Common Mistakes:
  • Counting duplicates as separate elements
  • Using preorder traversal which is not sorted
  • Sorting all values outside traversal (inefficient)
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