BST, Heaps & Map - DSA Interview Questions
Binary Search Trees (BST), Heaps, and Maps are fundamental data structures used for efficient searching, sorting, and storage of data. BSTs provide fast insertion, deletion, and search operations, Heaps enable priority-based data access, and Maps (HashMaps, TreeMaps) offer quick key-based retrieval. Mastering these structures is crucial for optimizing performance in complex coding problems.
Practice BST, Heaps & Map DSA Coding Problems with Solutions
Learning Objectives:
Understand the applications of BST, Heaps, and Maps in solving common programming challenges efficiently. Learn how to implement and optimize operations such as insertion, deletion, searching, and sorting using these data structures.
Exercise Instructions:
- Start with the first problem and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you with more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Insert a Node in a Binary Search Tree
Required Input:
root = [4, 2, 7, 1, 3] val = 5Expected Output:
[4, 2, 7, 1, 3, 5]
Code In Python
Run Code?
Click Run Button to view compiled output
2. Search for a Value in a BST
Required Input:
root = [4, 2, 7, 1, 3] target = 2Expected Output:
2
Code In Python
Run Code?
Click Run Button to view compiled output
3. Find the Minimum and Maximum Value in a BST
Required Input:
root = [4, 2, 7, 1, 3]Expected Output:
Minimum: 1 Maximum: 7
Code In Python
Run Code?
Click Run Button to view compiled output
4. Find the Inorder Successor of a Node in a BST
Required Input:
root = [20, 9, 25, 5, 12, None, None, None, None, 11, 14] target = 9Expected Output:
11
Code In Python
Run Code?
Click Run Button to view compiled output
5. Implement a Min Heap from Scratch
Required Input:
operations = ['insert 5', 'insert 3', 'insert 8', 'extract_min', 'get_min']Expected Output:
3
5
Code In Python
Run Code?
Click Run Button to view compiled output
6. Find the First Non-Repeating Character in a String Using a HashMap
Required Input:
s = 'swiss'Expected Output:
w
Code In Python
Run Code?
Click Run Button to view compiled output
7. Implement a Max Heap from Scratch
Required Input:
operations = ['insert 10', 'insert 15', 'insert 5', 'extract_max', 'get_max']Expected Output:
15
10
Code In Python
Run Code?
Click Run Button to view compiled output
8. Find the K Smallest Elements in an Array Using a Min Heap
Required Input:
arr = [7, 10, 4, 3, 20, 15] k = 3Expected Output:
[3, 4, 7]
Code In Python
Run Code?
Click Run Button to view compiled output
9. Find the Most Frequent Element in an Array Using a HashMap
Required Input:
arr = [1, 3, 2, 1, 4, 1, 3, 2, 3, 3, 3]Expected Output:
3
Code In Python
Run Code?
Click Run Button to view compiled output
10. Find the K Largest Elements in an Array Using a Max Heap
Required Input:
arr = [7, 10, 4, 3, 20, 15] k = 3Expected Output:
[20, 15, 10]
Code In Python
Run Code?
Click Run Button to view compiled output


