Remove Cancelled Items From a Singly Linked Cart
Last updated: November 29, 2026
Quick Overview
A cart is stored as a singly linked list of SKUs. Given a collection of cancelled SKUs, remove every node holding one of them, including at the head and consecutive runs, and return the new head.
Walmart Labs
November 29, 20260
0
0 solved
A cart is stored as a singly linked list of SKUs. Given a collection of cancelled SKUs, remove every node holding one of them, including at the head and consecutive runs, and return the new head.
A quick Walmart Labs phone screen warm-up that mirrors cleaning a Walmart.com cart after items go out of stock. The interviewer checks that you use a dummy node, handle consecutive removals without skipping nodes, and convert the cancelled list to a set.
What the Interviewer Expects
- Convert cancelled SKUs to a set for O(1) membership
- Use a dummy head to avoid special casing
- Do not advance the pointer after unlinking
- Handle an empty list and removing every node
- Return the possibly new head
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 do this recursively, and what is the risk?
- What if you must keep only the first occurrence of each SKU?
- How would you do this on a doubly linked list given direct node references?
- What if the cancelled set is very large and stored remotely?
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: head of a singly linked list with a sku field, and an iterable of cancelled SKUs. Output: the head after removing every node whose SKU is cancelled. Order of the remaining nodes is unchanged....
High-Level Design
A dummy node in front of head means every real node has a predecessor, so removing the head is the same as removing any other node. Walk with prev. If prev.next is cancelled, splice it out and sta...