Marathon Timing Strip Pair-Offs
Record adjacent-pair removals chosen by smallest value difference
A marathon timing crew stores calibration readings in a singly linked strip. Each node contains an integer reading. Number the nodes 0 through n - 1 in their original order; these position numbers never change.
The crew performs exactly rounds pair-offs. In each pair-off:
- Consider every pair of nodes that are adjacent in the current strip.
- Choose the pair with the smallest absolute difference between its readings.
- If several pairs tie, choose the one whose left node has the smallest original position number.
- Remove both nodes. Their surviving outside neighbors, if both exist, become adjacent.
Return a list of pairs of original position numbers, one pair for each pair-off in execution order. Within each returned pair, put the left node's position first.
The input head is the first node of the strip. There are always enough nodes to perform the requested number of pair-offs.
Examples
Example 1
Input: head = [8,1,2,9], rounds = 2 Output: [[1,2],[0,3]]
The first chosen pair has original positions 1 and 2 because its readings differ by 1. Removing it makes positions 0 and 3 adjacent, so those positions form the next pair.
Example 2
Input: head = [5,4,9,8,20], rounds = 2 Output: [[0,1],[2,3]]
Initially, the pairs at positions 0–1 and 2–3 both have difference 1. The left-position tie-break chooses 0–1. Among the remaining nodes, positions 2 and 3 are then chosen.
Example 3
Input: head = [7], rounds = 0 Output: []
No pair-offs are requested, so the removal record is empty and the strip is unchanged.
Constraints
- The linked list is acyclic and contains n nodes, where 0 <= n <= 100000.
- -1000000000 <= node.val <= 1000000000
- 0 <= rounds <= floor(n / 2)
- Original positions are zero-based and refer to the input order.
- The intended running time is O(n log n), with O(n) auxiliary space.
ListNode is predefined by the platform. The input list is supplied as a plain JSON list. Equal readings are separate nodes and retain distinct original positions. Only the removal record is returned.
Hints
Show hint 1Hint 1
Removing a pair destroys its incident adjacencies and creates at most one new adjacency. Maintain predecessor and successor links for surviving original positions.
Show hint 2Hint 2
Keep candidate adjacent pairs in a priority queue ordered by difference and original left position. When removing a candidate from the queue, verify that both endpoints still survive and are still adjacent.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you return the surviving linked list as well, while preserving its original node objects?
- How would the candidate validation change if readings could also be updated between pair-offs?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem