← Problems85. The basket of each orderMediumPySpark
00:00 / 20:00

The basket of each order

MediumNot attempted yetAsked at Amazon, Flipkart, Walmart

The recommender needs each order collapsed into the set of products it contained.

Input

order_items order_id bigint sku string -- null on a line the catalogue could not resolve qty bigint

Output

One row per order_id, with the columns:

  • order_id
  • skus — the distinct non-null skus, sorted ascending
  • sku_count — how many are in skus
  • total_qty — the sum of qty over every line, resolved or not

Example

order_idskuqty
5TEA1
5TEA2
5JAM1
order_idskussku_counttotal_qty
5[JAM, TEA]24

Constraints

  • The same product on two lines is one product, and both quantities count.
  • An order whose every line is unresolved still appears, with an empty skus, a sku_count of 0, and its quantities summed.
  • skus must be sorted. A set has no order, and an array in a different order is a different value however right its contents are.
  • Row order is not graded. Column names and their order are.

Example 1two orders, three lines

Input · order_items
qtyskuorder_id
2APPLE1
1BREAD1
3MILK2
Output
order_idskussku_counttotal_qty
1["APPLE","BREAD"]23
2["MILK"]13

Example 2the same product on two lines is one product

Input · order_items
qtyskuorder_id
1TEA5
2TEA5
1JAM5
Output
order_idskussku_counttotal_qty
5["JAM","TEA"]24

Topics

aggregationarray handling

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.