← Problems7. Flatten nested JSON into a fact tableMediumPython
00:00 / 20:00

Flatten nested JSON into a fact table

Medium38% solve itAsked at Uber

Explode a nested order payload into one row per line item, carrying the order-level attributes down onto every row.

Input schema

solve(payloads: list[dict]) -> list[dict] each payload: { "order_id": int, "customer_id": int, "ordered_at": str, "items": [{"sku": str, "quantity": int, "unit_price": float}, ...] }

Constraints

  • Column order is order_id, customer_id, ordered_at, sku, quantity, unit_price, line_total
  • line_total is quantity * unit_price, rounded to two decimals
  • An order with no items contributes no rows
  • Input order is preserved: orders in order, and items within an order in order

Example 1one order, two line items

Input · payloads
itemsorder_idordered_atcustomer_id
[{"sku":"A1","quantity":2,"unit_price":5},{"sku":"B2","quantity":1,"unit_price":12.5}]12024-01-01 09:00:0010
Output
order_idcustomer_idordered_atskuquantityunit_priceline_total
1102024-01-01 09:00:00A12510
1102024-01-01 09:00:00B2112.512.5

Example 2two orders keep their own attributes

Input · payloads
itemsorder_idordered_atcustomer_id
[{"sku":"X","quantity":1,"unit_price":3}]12024-02-01 00:00:007
[{"sku":"Y","quantity":3,"unit_price":2}]22024-02-02 00:00:008
Output
order_idcustomer_idordered_atskuquantityunit_priceline_total
172024-02-01 00:00:00X133
282024-02-02 00:00:00Y326

Topics

data modeling

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.