Bird
Raised Fist0
DSA Pythonprogramming~20 mins

BST Inorder Predecessor in DSA Python - 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
Find the inorder predecessor value in a BST
What is the output of the code that finds the inorder predecessor of node 15 in the given BST?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def inorder_predecessor(root, key):
    predecessor = None
    current = root
    while current:
        if key <= current.val:
            current = current.left
        else:
            predecessor = current
            current = current.right
    return predecessor.val if predecessor else None

# BST construction
root = Node(20)
root.left = Node(10)
root.right = Node(30)
root.left.left = Node(5)
root.left.right = Node(15)
root.left.right.left = Node(12)
root.left.right.right = Node(17)

print(inorder_predecessor(root, 15))
A17
B10
C12
D5
Attempts:
2 left
💡 Hint
Remember, the 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's value.
BIt is always the left child of the given node.
CIt is the node with the smallest value larger than the given node's value.
DIt is the rightmost node in the right subtree of the given node.
Attempts:
2 left
💡 Hint
Think about the inorder traversal order of BST nodes.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder predecessor function
What error will the following code produce when trying to find the inorder predecessor of 5 in the BST?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def inorder_predecessor_buggy(root, key):
    predecessor = None
    current = root
    while current:
        if key < current.val:
            current = current.left
        elif key > current.val:
            predecessor = current
            current = current.right
        else:
            if current.left:
                temp = current.left
                while temp.right:
                    temp = temp.right
                return temp.val
            break
    return predecessor.val if predecessor else None

root = Node(20)
root.left = Node(10)
root.right = Node(30)
root.left.left = Node(5)
root.left.right = Node(15)

print(inorder_predecessor_buggy(root, 5))
ANone
B10
C5
DAttributeError: 'NoneType' object has no attribute 'val'
Attempts:
2 left
💡 Hint
Check what happens when predecessor is None and you try to access its val.
❓ Predict Output
advanced
1:30remaining
Output of inorder predecessor for root node
What is the output of the code when finding the inorder predecessor of the root node (20) in the BST?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def inorder_predecessor(root, key):
    predecessor = None
    current = root
    while current:
        if key <= current.val:
            current = current.left
        else:
            predecessor = current
            current = current.right
    return predecessor.val if predecessor else None

root = Node(20)
root.left = Node(10)
root.right = Node(30)
root.left.left = Node(5)
root.left.right = Node(15)

print(inorder_predecessor(root, 20))
A15
B10
CNone
D5
Attempts:
2 left
💡 Hint
The predecessor is the largest value smaller than 20.
🚀 Application
expert
2:30remaining
Find inorder predecessor in BST with parent pointers
Given a BST node with a parent pointer, which approach correctly finds its inorder predecessor?
AIf node has right child, predecessor is leftmost node in right subtree; else move up parents until node is left child.
BIf node has left child, predecessor is rightmost node in left subtree; else move up parents until node is right child.
CPredecessor is always the left child of the node if it exists; otherwise None.
DPredecessor is the root node if node has no left child.
Attempts:
2 left
💡 Hint
Think about inorder traversal and how parent pointers help find predecessor.

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 inorder traversal order

    Inorder traversal visits nodes in ascending order for BST, so predecessor is the previous node in this order.
  2. Step 2: Define inorder predecessor

    The inorder predecessor is the node with the largest value smaller than the current node's value.
  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: Predecessor is just before current node in sorted order [OK]
Common Mistakes:
  • Confusing predecessor with successor
  • Thinking predecessor is always left child
  • Assuming predecessor is the parent node
2. Which of the following Python code snippets correctly finds the inorder predecessor of a node with a left subtree in a BST?
easy
A. temp = node.right while temp.right: temp = temp.right return temp
B. temp = node.right while temp.left: temp = temp.left return temp
C. temp = node.left while temp.left: temp = temp.left return temp
D. temp = node.left while temp.right: temp = temp.right return temp

Solution

  1. Step 1: Identify inorder predecessor logic for left subtree

    If node has left child, predecessor is the rightmost node in left subtree.
  2. Step 2: Check code correctness

    Code D moves to left child, then goes right as far as possible, correctly finding predecessor.
  3. Final Answer:

    Code snippet D correctly finds the inorder predecessor -> Option D
  4. Quick Check:

    Left subtree's rightmost node = predecessor [OK]
Hint: Go left once, then right as far as possible [OK]
Common Mistakes:
  • Going right subtree instead of left
  • Going left repeatedly instead of right in left subtree
  • Returning left child directly without traversal
3. Given the BST below and the node with value 15, what is the inorder predecessor?
       20
      /  \
    10    30
      \     \
      15    40
medium
A. 20
B. 15
C. 10
D. None

Solution

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

    Node 15 has no left child, so predecessor is ancestor smaller than 15.
  2. Step 2: Find nearest ancestor smaller than 15

    Ancestor 10 is smaller than 15 and is the closest on path to root.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Predecessor of 15 is 10 [OK]
Hint: If no left child, go up to smaller ancestor [OK]
Common Mistakes:
  • Choosing 20 which is larger
  • Choosing node itself as predecessor
  • Assuming predecessor is None without checking ancestors
4. The following code attempts to find the inorder predecessor of a node in a BST but has a bug. What is the bug?
def inorder_predecessor(root, node):
    if node.left:
        temp = node.left
        while temp.left:
            temp = temp.left
        return temp
    else:
        predecessor = None
        while root:
            if node.val > root.val:
                predecessor = root
                root = root.right
            else:
                root = root.left
        return predecessor
medium
A. The else block incorrectly updates predecessor
B. The while loop should check temp.right, not temp.left
C. The function does not handle empty tree
D. The function should return None if node has no left child

Solution

  1. Step 1: Analyze left subtree traversal

    In left subtree, predecessor is rightmost node, so loop should go right, not left.
  2. Step 2: Identify correct traversal direction

    Changing while temp.left to while temp.right fixes the bug.
  3. Final Answer:

    The while loop should check temp.right, not temp.left -> Option B
  4. Quick Check:

    Predecessor in left subtree = rightmost node [OK]
Hint: Go right in left subtree, not left [OK]
Common Mistakes:
  • Looping left instead of right in left subtree
  • Ignoring else block logic
  • Not returning predecessor correctly
5. You want to find the inorder predecessor of a node in a BST that does NOT have a left child. Which approach correctly finds the predecessor?
hard
A. Traverse from root, track the last node smaller than current node
B. Return the rightmost node in the left subtree
C. Return the parent node if it is smaller than the current node
D. Return the left child of the current node

Solution

  1. Step 1: Understand no left child case

    If node has no left child, predecessor is an ancestor smaller than node.
  2. Step 2: Use root traversal to track predecessor

    Traverse from root, update predecessor when node value is greater than root's value.
  3. Step 3: Return last tracked smaller ancestor

    This gives the inorder predecessor correctly.
  4. Final Answer:

    Traverse from root, track the last node smaller than current node -> Option A
  5. Quick Check:

    No left child -> track smaller ancestor [OK]
Hint: Track last smaller ancestor from root [OK]
Common Mistakes:
  • Returning parent without checking value
  • Returning left child when none exists
  • Trying to find predecessor in left subtree when none exists
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