Code Room
CodingHardcod-g1080
Subject Database secondary indexLevel Mid–Senior~30 minCommon in Databases & SQL interviewsIndustries Software development, Technology

Question

Simulate a secondary index. You are given rows as [row_id, attr] pairs where attr is an integer (row_id is a unique int). Build a secondary index mapping attr -> sorted list of row_ids. Then answer queries: each query is either ['eq', v] (all row_ids whose attr == v) or ['range', lo, hi] (all row_ids whose lo <= attr <= hi). For each query return the row_ids sorted ascending. Return a list of result lists, one per query.

Implement
secondary_index(rows: list[list[int]], queries: list[list]) → list[list[int]]
Examples
in[[[10,5],[11,5],[12,8],[13,3]],[["eq",5],["range",4,8]]]out[[10,11],[10,11,12]]
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.