Stable Partition of a Linked Queue Around a Priority Threshold
Last updated: November 29, 2026
Quick Overview
Given the head of a linked list of job priorities and a threshold x, reorder it so all nodes with value less than x come before nodes with value at least x, preserving relative order within each group.
Meta
November 29, 20260
0
0 solved
Given the head of a linked list of job priorities and a threshold x, reorder it so all nodes with value less than x come before nodes with value at least x, preserving relative order within each group.
A Meta phone-screen problem framed as promoting urgent jobs ahead of the rest in a linked work queue without disturbing their order. The interviewer checks that you build two sublists with dummy heads and terminate the second one to avoid a cycle.
What the Interviewer Expects
- Use two dummy heads for the small and large groups
- Append nodes to the correct tail in one pass
- Terminate the large list's tail
- Concatenate small then large
- Keep relative order stable
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
- Partition into three groups (less, equal, greater).
- Do it without dummy nodes.
- Why is stability important if downstream consumers rely on FIFO within a priority?
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 list and integer x. Output: head of the relinked list where every value < x precedes every value >= x, and within each group nodes keep their original order. Reuse existing nodes;...
High-Level Design
Walk the list once and detach each node onto one of two chains: small for values below x and large for the rest. Dummy heads mean each chain starts non-empty, so appending is always `tail.next = n...