Index range scans
A free medium coding practice problem from databases. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Medium
- Type
- Function
- Topic
- Databases
- Languages
- JavaScript, TypeScript or Python
Problem
keys is a sorted (non-decreasing) column of integers from a database index; ranges are queries [lo, hi] (lo ≤ hi). For each query return how many keys k satisfy lo ≤ k ≤ hi (like SELECT COUNT(*) … WHERE k BETWEEN lo AND hi), in query order. Values are within ±10^9; up to 50 000 keys and 50 000 queries — use the sort order (binary search), not a full scan per query.
Function signature
// JavaScript
function rangeCounts(keys, ranges)
// TypeScript
function rangeCounts(keys: any, ranges: any): any
// Python
def range_counts(keys, ranges):Examples
rangeCounts([1,3,3,5,8,13], [[3,5],[0,100],[6,7]])→[3,6,0]rangeCounts([], [[1,2]])→[0]
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.