Your question is Optimize Code Time and Space. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Lowe's runs seasonal bundle promotions where two SKUs whose prices sum exactly to a promo total get auto-linked as a suggested bundle. A merchandising analyst wrote the function below to find those matching price pairs from a store's price list, but on a full department price list (several thousand SKUs) it takes minutes to run and burns memory the team can't account for.
def find_bundle_pairs(prices, target_total):
all_pairs = []
for i in range(len(prices)):
for j in range(len(prices)):
if i != j:
all_pairs.append((i, j))
matches = []
for (i, j) in all_pairs:
if prices[i] + prices[j] == target_total:
matches.append((i, j))
unique_matches = []
for pair in matches:
if pair not in unique_matches and (pair[1], pair[0]) not in unique_matches:
unique_matches.append(pair)
return unique_matches
Explain the time and space complexity problems in this function and describe exactly how you would rewrite it so it scales to a full department price list.