Cart Merge
برمجةمكالمة هاتفيةسهلآخر مرة اتسأل من شهر
Problem
A shopper adds items as a guest, then logs in and already has a saved cart. Merge the two. Each cart is a list of {sku, qty} in the order items were added. Rules:
- Same
skuin both: quantities add up, capped bymax_qty[sku](default cap10if the SKU is not in the map). - Keep the user cart's order first, then guest-only SKUs in guest order.
- Drop any line whose final quantity is
0. - SKUs listed in
out_of_stockare dropped entirely and returned separately so the UI can show a message.
Return (merged, dropped).
Examples
Example 1
user = [{A, 2}, {B, 1}]
guest = [{B, 3}, {C, 1}]
max_qty = {B: 3}
→ merged [{A, 2}, {B, 3}, {C, 1}], dropped []. B would be 4 but is capped at 3.
Example 2
user = [{A, 1}]
guest = [{A, 0}, {D, 2}]
out_of_stock = {D}
→ merged [{A, 1}], dropped [D].
Constraints
- Up to
200lines per cart; SKUs are strings;0 <= qty <= 1000. - Single pass per cart is enough; do not go quadratic on SKU lookup.
- Be explicit about which cart wins on ties in ordering.
What they look for
An ordered map (or dict + list) so order is stable, the cap applied after summing, and the out-of-stock check done before the cap so the dropped list is accurate. The follow-up is usually about what happens if the user's cart changed on another device in between: version numbers or last-write-wins, and where that decision lives.