Code Room
CodingMediumcod-g1160
Subject Database indexLevel Mid–Senior~22 minCommon in Databases & SQL · Algorithms & data structures interviewsIndustries Software development

Question

A B-tree index stores integer keys in sorted order (duplicates allowed). Implement a range-scan cardinality estimate that returns the exact count of index entries with key in the inclusive range [lo, hi]. The keys are given already sorted ascending. You must run in O(log n) — do NOT linearly scan. If lo > hi, return 0. Up to 200000 keys.

Implement
index_range_count(keys: list[int], lo: int, hi: int) → int
Examples
in[[1,3,3,5,8,8,8,12],3,8]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.