Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

BST Inorder Predecessor in DSA Javascript - 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 of a node in BST
What is the output of the code when finding the inorder predecessor of node with value 15 in the given BST?
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insert(root, val) {
  if (!root) return new Node(val);
  if (val < root.val) root.left = insert(root.left, val);
  else root.right = insert(root.right, val);
  return root;
}

function inorderPredecessor(root, node) {
  let predecessor = null;
  while (root) {
    if (node.val <= root.val) {
      root = root.left;
    } else {
      predecessor = root;
      root = root.right;
    }
  }
  return predecessor ? predecessor.val : null;
}

const values = [20, 10, 30, 5, 15, 25, 35];
let root = null;
for (const v of values) {
  root = insert(root, v);
}
const node = { val: 15 };
console.log(inorderPredecessor(root, node));
A5
B20
C10
D15
Attempts:
2 left
💡 Hint
Remember, the inorder predecessor is the largest value smaller than the target node.
❓ Predict Output
intermediate
2:00remaining
Output of inorder predecessor when node has left subtree
What will be printed when finding the inorder predecessor of node with value 30 in this BST?
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insert(root, val) {
  if (!root) return new Node(val);
  if (val < root.val) root.left = insert(root.left, val);
  else root.right = insert(root.right, val);
  return root;
}

function findMax(node) {
  while (node.right) {
    node = node.right;
  }
  return node.val;
}

function inorderPredecessor(root, node) {
  if (node.left) {
    return findMax(node.left);
  }
  let predecessor = null;
  while (root) {
    if (node.val <= root.val) {
      root = root.left;
    } else {
      predecessor = root;
      root = root.right;
    }
  }
  return predecessor ? predecessor.val : null;
}

const values = [20, 10, 30, 25, 35];
let root = null;
for (const v of values) {
  root = insert(root, v);
}
const node = { val: 30, left: new Node(25) };
console.log(inorderPredecessor(root, node));
A20
B25
C10
D35
Attempts:
2 left
💡 Hint
If the node has a left subtree, the predecessor is the maximum value in that subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder predecessor function
What error will this code produce when trying to find the inorder predecessor of node with value 10?
DSA Javascript
function inorderPredecessor(root, node) {
  let predecessor = null;
  while (root) {
    if (node.val < root.val) {
      root = root.left;
    } else if (node.val > root.val) {
      predecessor = root;
      root = root.right;
    } else {
      if (root.left) {
        let temp = root.left;
        while (temp.right) {
          temp = temp.right;
        }
        return temp.val;
      }
      break;
    }
  }
  return predecessor ? predecessor.val : null;
}

const root = {
  val: 20,
  left: { val: 10, left: { val: 5, left: null, right: null }, right: { val: 15, left: null, right: null } },
  right: { val: 30, left: null, right: null }
};
const node = { val: 10 };
console.log(inorderPredecessor(root, node));
A5
B15
Cnull
DTypeError
Attempts:
2 left
💡 Hint
Check how the code finds the max value in the left subtree.
🧠 Conceptual
advanced
1:30remaining
Understanding inorder predecessor when no left subtree
If a node in a BST has no left child, how is its inorder predecessor found?
AThe rightmost ancestor whose left child is also an ancestor of the node
BThe leftmost ancestor whose left child is also an ancestor of the node
CThe rightmost ancestor whose right child is also an ancestor of the node
DThe leftmost ancestor whose right child is also an ancestor of the node
Attempts:
2 left
💡 Hint
Think about moving up the tree to find a smaller value than the node.
🚀 Application
expert
2:30remaining
Find inorder predecessor in BST with parent pointers
Given a BST node with parent pointers, which code snippet correctly finds its inorder predecessor?
A
function inorderPredecessor(node) {
  if (node.left) {
    let curr = node.left;
    while (curr.right) curr = curr.right;
    return curr;
  }
  let curr = node;
  while (curr.parent &amp;&amp; curr === curr.parent.left) {
    curr = curr.parent;
  }
  return curr.parent;
}
B
function inorderPredecessor(node) {
  if (node.right) {
    let curr = node.right;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let curr = node;
  while (curr.parent &amp;&amp; curr === curr.parent.right) {
    curr = curr.parent;
  }
  return curr.parent;
}
C
function inorderPredecessor(node) {
  if (node.left) {
    let curr = node.left;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let curr = node;
  while (curr.parent &amp;&amp; curr === curr.parent.right) {
    curr = curr.parent;
  }
  return curr.parent;
}
D
function inorderPredecessor(node) {
  if (node.left) {
    let curr = node.left;
    while (curr.right) curr = curr.right;
    return curr;
  }
  let curr = node;
  while (curr.parent &amp;&amp; curr === curr.parent.right) {
    curr = curr.parent;
  }
  return curr.parent;
}
Attempts:
2 left
💡 Hint
Remember the inorder predecessor is the max node in left subtree or the first ancestor where node is in right subtree.

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 root node of the BST
D. The left child of the given node

Solution

  1. Step 1: Understand BST ordering

    In a BST, left children are smaller and right children are larger than the 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 value [OK]
Common Mistakes:
  • Confusing predecessor with successor
  • Thinking predecessor is always left child
  • Assuming predecessor is the root node
2. Which of the following is the correct JavaScript function signature to find the inorder predecessor of a node in a BST?
easy
A. function findPredecessor(node) {}
B. function inorderSuccessor(root, node) {}
C. function findInorderPredecessor(root, node) {}
D. function getInorderPredecessor(node, root) {}

Solution

  1. Step 1: Check function purpose and parameters

    The function should find the inorder predecessor given the BST root and the target node.
  2. Step 2: Match signature to purpose

    function findInorderPredecessor(root, node) {} correctly uses root and node as parameters and names the function appropriately.
  3. Final Answer:

    function findInorderPredecessor(root, node) {} -> Option C
  4. Quick Check:

    Correct function name and parameters [OK]
Hint: Function needs root and node parameters [OK]
Common Mistakes:
  • Using successor function name instead of predecessor
  • Swapping parameter order
  • Missing root parameter
3. Given the BST below and the node with value 15, what is the inorder predecessor?
       20
      /  \
    10    30
      \     \
      15     40
medium
A. 10
B. 15
C. 20
D. 30

Solution

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

    Node 15 has no left child, so we look upwards to find the largest smaller ancestor.
  2. Step 2: Find largest smaller ancestor

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

    10 -> Option A
  4. Quick Check:

    Largest smaller value than 15 is 10 [OK]
Hint: If no left child, go up to largest smaller ancestor [OK]
Common Mistakes:
  • Choosing the node itself as predecessor
  • Choosing the parent if it is larger
  • Confusing successor with predecessor
4. The following JavaScript code tries to find the inorder predecessor of a node in a BST but has a bug. What is the bug?
function findInorderPredecessor(root, node) {
  if (node.left) {
    let temp = node.left;
    while (temp.right) {
      temp = temp.right;
    }
    return temp;
  }
  let predecessor = null;
  let current = root;
  while (current) {
    if (node.val > current.val) {
      predecessor = current;
      current = current.right;
    } else {
      current = current.left;
    }
  }
  return predecessor;
}
medium
A. It does not handle the case when node has no left child
B. It does not check if node exists in the BST
C. It should return null if predecessor is not found
D. It incorrectly updates predecessor when node.val <= current.val

Solution

  1. Step 1: Analyze code logic for predecessor

    The code correctly finds predecessor if node has left child or searches ancestors otherwise.
  2. Step 2: Identify missing validation

    Code does not verify if the given node actually exists in the BST, which can cause incorrect results or errors.
  3. Final Answer:

    It does not check if node exists in the BST -> Option B
  4. Quick Check:

    Missing node existence check [OK]
Hint: Always verify node exists in BST before predecessor search [OK]
Common Mistakes:
  • Assuming node is always in BST
  • Confusing left child check logic
  • Ignoring null return possibility
5. You want to find the inorder predecessor of the node with value 25 in the BST below:
          40
         /  \
       20    60
      /  \  /  \
    10   30 50  70
         / 
        25
Which node is the inorder predecessor of 25, and why?
hard
A. 10, because it is the smallest node smaller than 25
B. 30, because it is the parent of 25
C. 25 has no inorder predecessor
D. 20, because it is the largest smaller ancestor

Solution

  1. Step 1: Check if node 25 has left child

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

    Ancestors of 25 are 30, 20, and 40. Among these, 30 is the parent of 25 and the largest smaller ancestor is 20, but since 25 is in the left subtree of 30, the inorder predecessor is 30's left subtree's rightmost node or the parent if no left subtree exists. Here, 25 is left child of 30, so predecessor is 20.
  3. Final Answer:

    20, because it is the largest smaller ancestor -> Option D
  4. Quick Check:

    Largest smaller ancestor = 20 [OK]
Hint: If no left child, predecessor is largest smaller ancestor [OK]
Common Mistakes:
  • Choosing parent node 30 incorrectly
  • Thinking no predecessor exists
  • Choosing smallest smaller node instead of largest
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