Sort a Linked List of Event Timestamps With Constant Extra Space

Last updated: November 29, 2026

Quick Overview

Sort a singly linked list of event timestamps in ascending order in O(n log n) time using O(1) extra space (no recursion stack).

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Sort a singly linked list of event timestamps in ascending order in O(n log n) time using O(1) extra space (no recursion stack).

A Meta onsite question framed around ordering out-of-order events buffered in a linked log before flushing. Interviewers expect merge sort as the right algorithm for linked lists and push for the bottom-up version to remove the O(log n) recursion stack.

What the Interviewer Expects
  • Explain why merge sort suits linked lists (no random access needed, stable)
  • Write a clean merge of two sorted lists
  • Implement a split that cuts off a run of a given size
  • Do bottom-up passes doubling the run size
  • State O(n log n) time and O(1) extra space
Key Topics to Cover
Linked list
Merge sort
Bottom-up
Stability
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
  • Write the simpler top-down recursive version and compare.
  • Why is quicksort a worse fit for linked lists?
  • How would you sort a log too large for memory?
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 list of n integers (timestamps, duplicates possible). Output: head of the same nodes relinked in non-decreasing order. Target O(n log n) time and O(1) auxiliary space, which rules o...

High-Level Design

Bottom-up merge sort. Count n. For size = 1, 2, 4, ... while size < n, make a pass: starting from a dummy, repeatedly cut off a run left of length size, then a run right of length size, merge th...


Submit Your Answer
Markdown supported