Code RoomWhere-clause predicate tree
HardPrep Room Coding #247

Where-clause predicate tree

CodingDatabases & SQLAlgorithms & data structuresMid–Senior~30 min

Evaluate a WHERE-clause predicate tree against rows. A predicate node is either a comparison ['cmp', col, op, value] with op in '=','!=','>','<','>=','<=', or a boolean ['and', child1, child2, ...] / ['or', child1, child2, ...] / ['not', child]. Given rows (list of dicts) and one predicate, return the list of rows (in input order) for which the predicate evaluates to true. Comparisons use Python's natural ordering on the stored values. 'and' of zero children is true; 'or' of zero children is false.

Implement
filter_rows(rows: list[dict], pred: list) → list[dict]
Examples
in[[{"a":1,"b":10},{"a":2,"b":5},{"a":3,"b":20}],["and",["cmp","a",">",1],["cmp","b",">=",10]]]out[{"a":3,"b":20}]
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
InputExpectedGot
[[{"a":1,"b":10},{"a":2,"b":5},{"a":3,"b":20}],["and",["cmp","a",">",1],["cmp","b",">=",10]]][{"a":3,"b":20}]not run yetsample