Partition a Shipment List Around a Weight Threshold Stably

Last updated: November 29, 2026

Quick Overview

Given a singly linked list of shipment weights and a threshold x, rearrange it so all nodes with weight less than x come before nodes with weight greater than or equal to x. Preserve the original relative order inside each group.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a singly linked list of shipment weights and a threshold x, rearrange it so all nodes with weight less than x come before nodes with weight greater than or equal to x. Preserve the original relative order inside each group.

A fulfillment framing of stable list partitioning: light parcels go to one sorter lane ahead of heavy ones. Amazon interviewers check that you keep order stable, avoid allocating new nodes, and terminate the tail to avoid cycles.

What the Interviewer Expects
  • Build two chains with their own dummy heads
  • Append each node to the correct chain in a single pass
  • Terminate the second chain with None
  • Join the chains and return the new head
  • Confirm stability with an example containing duplicates
Key Topics to Cover
linked lists
stable partition
dummy nodes
single pass
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you partition into three groups (light, medium, heavy)?
  • Can you do it without dummy nodes?
  • How does this relate to the partition step of quicksort, and why is that one not stable?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: head of a singly linked list of integers and threshold x. Output: the head of the rearranged list where every node with value < x precedes every node with value >= x, and inside each group no...

High-Level Design

Keep two growing chains: low for values under the threshold and high for the rest, each starting from a dummy node with a tail pointer. Walk the input once and append each node to the tail of its ...


Submit Your Answer
Markdown supported