Remove Cancelled Orders From a Linked Dispatch Queue
Last updated: November 29, 2026
Quick Overview
A dispatch queue is stored as a singly linked list of order ids. Given the head and a set of cancelled order ids, remove every cancelled node in one pass and return the new head.
DoorDash
November 29, 20260
0
0 solved
A dispatch queue is stored as a singly linked list of order ids. Given the head and a set of cancelled order ids, remove every cancelled node in one pass and return the new head.
A linked list warm-up framed around the queue of orders waiting for Dasher assignment, where cancellations must be removed without rebuilding the queue. DoorDash interviewers use it to see clean pointer handling, a dummy head to avoid special cases, and correct behavior when the head or consecutive nodes are removed.
What the Interviewer Expects
- Use a dummy node so removing the head needs no special case
- Only advance prev when the current node is kept
- Handle consecutive removals correctly
- Use a set for O(1) membership
- Return the new head, which may be None
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 on a doubly linked list given direct references to nodes?
- What if cancellations arrive while another thread is reading the queue?
- How would you remove only the first occurrence of each cancelled id?
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 (
order_id,next) and a setcancelled. - Remove all nodes whose id is in the set, keep the rest in order.
- Return the new head (possibly
None). O(1) extra...
High-Level Design
Place a dummy node before the head. Keep prev (last kept node) and cur. If cur is cancelled, splice it out with prev.next = cur.next and do not move prev, because the next node might als...