Cheapest stock items
A grocery app offers replacements when a shopper's item is unavailable. skus holds the candidate product codes, cents holds each candidate's shelf price in cents, in_stock says whether the warehouse can actually pick that candidate, and the three lists are the same length. Return the product codes of the k cheapest candidates the warehouse can pick, cheapest first. Candidates that are not in stock are dropped before anything is chosen, so k counts only pickable ones. Two candidates on the same price are ordered by product code ascending as ordinary strings, compared character by character, so SKU-10 comes before SKU-9. Product codes are distinct. When fewer than k candidates survive the stock filter, return every survivor in that order, and when k is zero or negative return an empty list.
pick_cheapest_substitutes(skus: list[str], cents: list[int], in_stock: list[bool], k: int) → list[str][["SKU-9","SKU-10","SKU-3"],[500,500,450],[true,true,true],2]out["SKU-3","SKU-10"][["OAT-1","SOY-2","ALM-3","RIC-4"],[320,280,280,199],[true,false,true,true],3]out["RIC-4","ALM-3","OAT-1"][["A","B","C"],[10,20,30],[false,true,true],1]out["B"]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.
[["SKU-9","SKU-10","SKU-3"],[500,500,450],[true,true,true],2]["SKU-3","SKU-10"]not run yetsample[["OAT-1","SOY-2","ALM-3","RIC-4"],[320,280,280,199],[true,false,true,true],3]["RIC-4","ALM-3","OAT-1"]not run yetsample[["A","B","C"],[10,20,30],[false,true,true],1]["B"]not run yetsample