Group Shopping Carts That Contain the Same Item Multiset
Last updated: November 29, 2026
Quick Overview
Given a map from cart id to the list of SKUs in that cart (an SKU may appear more than once), return every group of two or more carts holding exactly the same items with the same quantities. Sort ids within each group and sort the groups.
Walmart Labs
November 29, 20260
0
0 solved
Given a map from cart id to the list of SKUs in that cart (an SKU may appear more than once), return every group of two or more carts holding exactly the same items with the same quantities. Sort ids within each group and sort the groups.
A warm-up in the style of Walmart Labs online assessments, framed around cart analytics for Walmart.com where identical baskets reveal bundling opportunities. The grader checks that you build a canonical key that respects quantities and that you keep the pass linear in the total number of items.
What the Interviewer Expects
- Explain why a plain set loses quantity information
- Build a canonical, hashable key from SKU counts
- Group in one pass with a dictionary of lists
- Return deterministic output ordering
- State time and space complexity
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you group carts that differ only by one item?
- What if there are 200 million carts spread across many machines?
- How would you treat substitutions such as two SKUs that are the same product in different sizes?
- Could you use sorting of the SKU list instead of counting, and what does it cost?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a dict cart_id -> list[str] of SKUs. Two carts match only if every SKU appears the same number of times in both, so [milk, eggs] and [milk, milk, eggs] are different. Output is a sorted...
High-Level Design
Turn each cart into a canonical key that is identical for equal multisets. Counting SKUs and sorting the (sku, count) pairs gives a tuple that is hashable and order independent. A frozenset would ...