Bird
Raised Fist0
DSA Rustprogramming~20 mins

BST Inorder Predecessor in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Inorder Predecessor Function Call

Given a Binary Search Tree (BST), what is the output of the inorder predecessor function when called on the node with value 15?

DSA Rust
struct TreeNode {
    val: i32,
    left: Option<Box<TreeNode>>,
    right: Option<Box<TreeNode>>,
}

fn inorder_predecessor(root: &Option<Box<TreeNode>>, key: i32) -> Option<i32> {
    let mut predecessor = None;
    let mut current = root;
    while let Some(node) = current {
        if key <= node.val {
            current = &node.left;
        } else {
            predecessor = Some(node.val);
            current = &node.right;
        }
    }
    predecessor
}

fn main() {
    let root = Some(Box::new(TreeNode {
        val: 20,
        left: Some(Box::new(TreeNode {
            val: 10,
            left: Some(Box::new(TreeNode { val: 5, left: None, right: None })),
            right: Some(Box::new(TreeNode { val: 15, left: None, right: None })),
        })),
        right: Some(Box::new(TreeNode {
            val: 30,
            left: None,
            right: None,
        })),
    }));
    let pred = inorder_predecessor(&root, 15);
    println!("{:?}", pred);
}
ANone
BSome(5)
CSome(15)
DSome(10)
Attempts:
2 left
💡 Hint

Inorder predecessor is the largest value smaller than the given key.

🧠 Conceptual
intermediate
1:30remaining
Understanding Inorder Predecessor in BST

Which statement correctly describes the inorder predecessor of a node in a BST?

AIt is the node with the largest value smaller than the given node.
BIt is the rightmost node in the right subtree of the given node.
CIt is always the left child of the given node.
DIt is the node with the smallest value greater than the given node.
Attempts:
2 left
💡 Hint

Think about the inorder traversal order.

🔧 Debug
advanced
2:00remaining
Identify the Bug in Inorder Predecessor Code

What error will occur when running the following Rust code to find the inorder predecessor?

DSA Rust
fn inorder_predecessor(root: &Option<Box<TreeNode>>, key: i32) -> Option<i32> {
    let mut predecessor = None;
    let mut current = root;
    while let Some(node) = current {
        if key < node.val {
            current = &node.left;
        } else {
            predecessor = Some(node.val);
            current = &node.right;
        }
    }
    predecessor
}
AThe function returns the correct predecessor for all keys.
BThe function causes a stack overflow due to infinite recursion.
CThe function returns None for keys equal to node values, missing correct predecessor.
DThe function panics due to unwrapping a None value.
Attempts:
2 left
💡 Hint

Check the condition for moving left or right in the loop.

📝 Syntax
advanced
1:30remaining
Syntax Error in Rust BST Predecessor Code

Which option contains the correct Rust syntax to define a BST node struct with optional left and right children?

A
struct TreeNode {
    val: i32,
    left: Box&lt;TreeNode&gt;,
    right: Box&lt;TreeNode&gt;,
}
B
struct TreeNode {
    val: i32,
    left: Option&lt;Box&lt;TreeNode&gt;&gt;,
    right: Option&lt;Box&lt;TreeNode&gt;&gt;,
}
C
struct TreeNode {
    val: i32,
    left: Option&lt;TreeNode&gt;,
    right: Option&lt;TreeNode&gt;,
}
D
struct TreeNode {
    val: i32,
    left: Box&lt;Option&lt;TreeNode&gt;&gt;,
    right: Box&lt;Option&lt;TreeNode&gt;&gt;,
}
Attempts:
2 left
💡 Hint

Children must be optional and boxed to allow recursive types.

🚀 Application
expert
2:30remaining
Find Inorder Predecessor in Complex BST

Given the BST below, what is the inorder predecessor of the node with value 25?

BST structure:

  • Root: 20
  • Left subtree: 10 (left: 5, right: 15)
  • Right subtree: 30 (left: 25, right: 35)
A20
B25
C15
D30
Attempts:
2 left
💡 Hint

Inorder predecessor is the largest value smaller than the node's value.

Practice

(1/5)
1. What is the inorder predecessor of a node in a Binary Search Tree (BST)?
easy
A. The node with the largest value smaller than the given node
B. The node with the smallest value larger than the given node
C. The left child of the given node
D. The right child of the given node

Solution

  1. Step 1: Understand BST property

    In a BST, left subtree nodes are smaller and right subtree nodes are larger than the current node.
  2. Step 2: Define inorder predecessor

    The inorder predecessor is the node visited just before the current node in an inorder traversal, which means it has the largest value smaller than the current node.
  3. Final Answer:

    The node with the largest value smaller than the given node -> Option A
  4. Quick Check:

    Inorder predecessor = largest smaller value [OK]
Hint: Inorder predecessor is largest smaller node [OK]
Common Mistakes:
  • Confusing predecessor with successor
  • Choosing child nodes directly without checking values
  • Thinking predecessor is always left child
2. Which Rust code snippet correctly finds the inorder predecessor of a node with a left subtree in a BST?
easy
A. let pred = node.right.clone();
B. let mut pred = node.right.as_ref(); while let Some(left) = pred.unwrap().left.as_ref() { pred = Some(left); }
C. let mut pred = node.left.as_ref(); while let Some(right) = pred.unwrap().right.as_ref() { pred = Some(right); }
D. let pred = node.left.clone();

Solution

  1. Step 1: Identify left subtree traversal

    The inorder predecessor is the rightmost node in the left subtree, so start from node.left.
  2. Step 2: Traverse to rightmost node

    Keep moving to the right child until no more right child exists to find the largest node in left subtree.
  3. Final Answer:

    let mut pred = node.left.as_ref(); while let Some(right) = pred.unwrap().right.as_ref() { pred = Some(right); } -> Option C
  4. Quick Check:

    Traverse left subtree rightmost node = let mut pred = node.left.as_ref(); while let Some(right) = pred.unwrap().right.as_ref() { pred = Some(right); } [OK]
Hint: Go left once, then right as far as possible [OK]
Common Mistakes:
  • Starting from right subtree instead of left
  • Not looping to the rightmost node
  • Using clone instead of reference traversal
3. Given the BST below and node value 15, what is the inorder predecessor?

       20
      /  \
    10    30
      \     \
      15    40
medium
A. 30
B. 15
C. 20
D. 10

Solution

  1. Step 1: Locate node 15 and its left subtree

    Node 15 has no left child, so we look for the ancestor smaller than 15.
  2. Step 2: Find largest ancestor smaller than 15

    10 is the parent of 15 and smaller than 15, so 10 is the inorder predecessor.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Largest smaller ancestor = 10 [OK]
Hint: If no left child, go up to smaller ancestor [OK]
Common Mistakes:
  • Choosing node itself as predecessor
  • Choosing larger ancestor incorrectly
  • Ignoring no left subtree case
4. Identify the bug in this Rust function to find the inorder predecessor of a BST node:

fn inorder_predecessor(node: &Option) -> Option<i32> {
    let mut current = node.as_ref()?.left.as_ref();
    while current?.right.is_some() {
        current = current?.right.as_ref();
    }
    current.map(|n| n.val)
}
medium
A. Using '?' operator on Option without handling None case
B. Starting from node.right instead of node.left
C. Returning node.val instead of predecessor value
D. Not checking if node has left child before traversal

Solution

  1. Step 1: Analyze use of '?' operator in loop condition

    The '?' operator unwraps Option or returns None; using it inside while condition causes panic if None.
  2. Step 2: Identify safe way to traverse Option references

    Must use pattern matching or if let to safely unwrap Options instead of '?' in loop condition.
  3. Final Answer:

    Using '?' operator on Option without handling None case -> Option A
  4. Quick Check:

    Unsafe '?' in loop condition causes panic [OK]
Hint: Avoid '?' in loop conditions on Option [OK]
Common Mistakes:
  • Using '?' inside loops causing runtime panic
  • Ignoring None cases in Option traversal
  • Confusing left and right subtree
5. You want to find the inorder predecessor of a node in a BST that has no left child. Which approach correctly finds it?
hard
A. Return the rightmost node in the right subtree
B. Traverse up the tree to find the nearest ancestor whose right child is also an ancestor of the node
C. Return the leftmost node in the right subtree
D. Return the root node always

Solution

  1. Step 1: Understand no left child case

    If node has no left child, predecessor is not in left subtree but among ancestors.
  2. Step 2: Find nearest ancestor where node is in right subtree

    Traverse up until you find an ancestor whose right child is also an ancestor of the node; that ancestor is the predecessor.
  3. Final Answer:

    Traverse up the tree to find the nearest ancestor whose right child is also an ancestor of the node -> Option B
  4. Quick Check:

    No left child -> find right-side ancestor [OK]
Hint: Go up to ancestor where node is in right subtree [OK]
Common Mistakes:
  • Returning nodes from right subtree incorrectly
  • Always returning root node
  • Ignoring ancestor 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