Code Room
CodingHardcod-g555
Subject Segment treeLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development, Technology

Question

Given a static integer array and a list of [l, r, x] queries, return for each query the count of elements in the inclusive range arr[l..r] whose value is <= x. Build a merge-sort tree (a segment tree whose every node stores its segment's elements in sorted order); a query decomposes [l, r] into O(log n) nodes and binary-searches each sorted list for x, giving O(log^2 n) per query. Return the list of counts in query order.

Implement
range_count_leq(arr: list[int], queries: list[list[int]]) → list[int]
Examples
in[[1,5,2,8,3,7],[[0,5,4],[1,3,5],[2,2,2],[0,5,10]]]out[3,2,1,6]
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.