Sort a Huge Linked Event List With Constant Extra Memory

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 only O(1) extra memory, which rules out recursion and copying to an array.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Sort a singly linked list of event timestamps in ascending order in O(n log n) time using only O(1) extra memory, which rules out recursion and copying to an array.

A Google onsite question that pushes past the familiar recursive merge sort. The interviewer asks for O(1) space to see whether you can write bottom-up merge sort with explicit split and merge helpers and keep the bookkeeping straight.

What the Interviewer Expects
  • Choose merge sort since linked lists merge without extra arrays
  • Explain why top-down recursion costs O(log n) stack
  • Implement bottom-up passes with run width 1, 2, 4, ...
  • Write split(head, n) that cuts after n nodes and returns the rest
  • Merge two runs and return both head and tail to stitch passes
Key Topics to Cover
linked list
merge sort
bottom-up
constant space
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
  • Make the sort stable and prove it.
  • Why is quicksort a poor fit for linked lists?
  • Events arrive continuously; maintain a sorted list with good amortized cost.
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 comparable values.
  • Output: head of the sorted list using the same nodes.
  • O(n log n) time, O(1) extra space....
High-Level Design

Count the length n. Then run passes with width = 1, 2, 4, ... until width >= n. Each pass walks the list from a dummy: cut off a run of width nodes (left), cut off the next run of width nodes (r...


Submit Your Answer
Markdown supported