Pivot long-format records
Pivot a list of long-format records into a wide table. Each record is a dict with an `index_key`, a `col_key`, and a numeric `val_key`. Produce a 2D table whose first row is a header `['_', col1, col2, ...]` with the distinct column-key values sorted ascending, followed by one row per distinct index value (sorted ascending): `[index_value, v_col1, v_col2, ...]`, using `0` where a given (index, column) pair has no record. Return the table as a list of lists.
Implement
pivot(rows: list[dict], index_key: str, col_key: str, val_key: str) → list[list]Examples
in
[[{"d":"mon","n":1,"p":"x"},{"d":"mon","n":2,"p":"y"},{"d":"tue","n":3,"p":"x"}],"d","p","n"]out[["_","x","y"],["mon",1,2],["tue",3,0]]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
[[{"d":"mon","n":1,"p":"x"},{"d":"mon","n":2,"p":"y"},{"d":"tue","n":3,"p":"x"}],"d","p","n"][["_","x","y"],["mon",1,2],["tue",3,0]]not run yetsample