Secondary index queries
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.
0:00 of about 30 min
solution.py
InputExpectedGot
[[[10,5],[11,5],[12,8],[13,3]],[["eq",5],["range",4,8]]][[10,11],[10,11,12]]not run yetsample