Code RoomPivot sparse records
HardPrep Room Coding #788

Pivot sparse records

CodingAlgorithms & data structuresSenior–Staff~35 min

Given a list of records, each a dict with keys 'row', 'col', and 'val' (int), pivot them into a dense matrix. Collect the distinct 'row' values and distinct 'col' values, sort each ascending, and build a 2D list where entry [i][j] is the sum of all 'val' for records whose row equals the i-th sorted row label and whose col equals the j-th sorted col label. Cells with no record are 0. Return a dict with keys 'rows' (sorted row labels), 'cols' (sorted col labels), and 'matrix' (the 2D list). The input may be empty (return empty lists for all three).

Implement
pivot_sum(records: list[dict]) → dict
Examples
in[[{"col":"a","row":"x","val":1},{"col":"b","row":"x","val":2},{"col":"a","row":"y","val":3}]]out{"cols":["a","b"],"rows":["x","y"],"matrix":[[1,2],[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 35 min
InputExpectedGot
[[{"col":"a","row":"x","val":1},{"col":"b","row":"x","val":2},{"col":"a","row":"y","val":3}]]{"cols":["a","b"],"rows":["x","y"],"matrix":[[1,2],[3,0]]}not run yetsample