Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Function
What is the output of the following C code that finds the 3rd smallest element in the given BST?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
int count = 0;
int result = -1;
void kthSmallestUtil(Node* root, int k) {
if (!root) return;
kthSmallestUtil(root->left, k);
count++;
if (count == k) {
result = root->val;
return;
}
kthSmallestUtil(root->right, k);
}
int kthSmallest(Node* root, int k) {
count = 0;
result = -1;
kthSmallestUtil(root, k);
return result;
}
// BST structure:
// 5
// / \
// 3 7
// / \ \
// 2 4 8
// Call: kthSmallest(root, 3);Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST gives sorted order.
✗ Incorrect
The inorder traversal visits nodes in ascending order: 2, 3, 4, 5, 7, 8. The 3rd smallest is 4.
🧠 Conceptual
intermediateUnderstanding Kth Smallest Element in BST
Which property of a Binary Search Tree (BST) allows us to find the kth smallest element efficiently using inorder traversal?
Attempts:
2 left
💡 Hint
Think about the order in which nodes are visited in inorder traversal.
✗ Incorrect
Inorder traversal of BST visits nodes from smallest to largest, which helps find the kth smallest element.
🔧 Debug
advancedIdentify the Bug in Kth Smallest Element Code
What error will occur when running this code to find the kth smallest element in a BST?
DSA C
int kthSmallestUtil(Node* root, int k, int* count) { if (!root) return -1; int left = kthSmallestUtil(root->left, k, count); if (left != -1) return left; (*count)++; if (*count == k) return root->val; return kthSmallestUtil(root->right, k, count); } int kthSmallest(Node* root, int k) { int count = 0; return kthSmallestUtil(root, k, &count); } // Suppose k is larger than number of nodes in BST.
Attempts:
2 left
💡 Hint
What happens if k is bigger than total nodes?
✗ Incorrect
If k is larger than number of nodes, function returns -1 to indicate kth element not found.
❓ Predict Output
advancedOutput of Iterative Kth Smallest Element Code
What is the output of this iterative C code to find the 2nd smallest element in the BST below?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int val; struct Node* left; struct Node* right; } Node; typedef struct Stack { Node* data[100]; int top; } Stack; void push(Stack* s, Node* node) { s->data[++(s->top)] = node; } Node* pop(Stack* s) { return s->data[(s->top)--]; } int kthSmallest(Node* root, int k) { Stack s = {.top = -1}; Node* curr = root; int count = 0; while (curr || s.top != -1) { while (curr) { push(&s, curr); curr = curr->left; } curr = pop(&s); count++; if (count == k) return curr->val; curr = curr->right; } return -1; } // BST: // 6 // / \ // 3 8 // / \ \ // 2 4 9 // Call: kthSmallest(root, 2);
Attempts:
2 left
💡 Hint
Iterative inorder traversal visits nodes in ascending order.
✗ Incorrect
Inorder traversal order: 2, 3, 4, 6, 8, 9. The 2nd smallest is 3.
🚀 Application
expertKth Smallest Element with Augmented BST
In an augmented BST where each node stores the count of nodes in its left subtree, how can you find the kth smallest element efficiently?
Attempts:
2 left
💡 Hint
Use the stored counts to skip subtrees.
✗ Incorrect
Using left subtree counts, we decide whether kth smallest is in left subtree, current node, or right subtree, reducing search time.
