Code RoomCheapest product in each category
MediumPrep Room Coding #5020

Cheapest product in each category

CodingAlgorithms & data structuresMid–Senior~9 min

Given `products` with columns `sku`, `category`, `price` and `stock`, return the single cheapest product per category, breaking ties by `sku` ascending. Return `category`, `sku` and `price`, sorted by `category` ascending.

Implement
cheapest_per_category(products: dataframe) → dataframe
Examples
in[{"__df__":[{"sku":"A-1","price":9.99,"stock":12,"category":"tools"},{"sku":"B-2","price":24.5,"stock":0,"category":"tools"},{"sku":"C-3","price":15,"stock":7,"category":"garden"},{"sku":"D-4","price":15,"stock":3,"category":"garden"},{"sku":"E-5","price":42,"stock":0,"category":"kitchen"}]}]out[{"sku":"C-3","price":15,"category":"garden"},{"sku":"E-5","price":42,"category":"kitchen"},{"sku":"A-1","price":9.99,"category":"tools"}]
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 9 min
InputExpectedGot
[{"__df__":[{"sku":"A-1","price":9.99,"stock":12,"category":"tools"},{"sku":"B-2","price":24.5,"stock":0,"category":"tools"},{"sku":"C-3","price":15,"stock":7,"category":"garden"},{"sku":"D-4","price":15,"stock":3,"category":"garden"},{"sku":"E-5","price":42,"stock":0,"category":"kitchen"}]}][{"sku":"C-3","price":15,"category":"garden"},{"sku":"E-5","price":42,"category":"kitchen"},{"sku":"A-1","price":9.99,"category":"tools"}]not run yetsample