Dsa
Binary Search Tree
Medium
The Range-Restricted Median
Given a range [L, R] and a sequence of integers to build a BST, find the median value of all nodes in the BST whose values fall within the range [L, R] (inclusive).
To calculate the median: Sort the valid node values in ascending order. The median is the element at index floor((K-1)/2), where K is the number of valid nodes.
Example:
Input:
5 15
10 2 12 7 18 14
Values in [5, 15]: 10, 12, 7, 14.
Sorted: 7, 10, 12, 14. K=4. Index = floor(3/2) = 1.
Output: 10
Key concepts
binary_search_treeinorder_traversalrange_query
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now