Code Room
CodingHardcod-g214
Subject Data wranglingLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

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.