Remove Expired Sessions From a Linked List In Place

Last updated: November 29, 2026

Quick Overview

Given the head of a singly linked list of chat sessions, each with an expiry timestamp in no particular order, remove every session that has expired as of time `now` in place, keeping the survivors in their original order, and return the new head.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the head of a singly linked list of chat sessions, each with an expiry timestamp in no particular order, remove every session that has expired as of time `now` in place, keeping the survivors in their original order, and return the new head.

Session tables and connection pools in OpenAI's serving stack are periodically swept for expired entries, and linked structures are still common inside such pools. This warm-up checks pointer manipulation without leaks or skipped nodes, use of a dummy head, and testing of runs of consecutive deletions.

What the Interviewer Expects
  • Use a dummy head so deleting the first node is not a special case
  • Advance the trailing pointer only when the current node is kept
  • Handle consecutive expired nodes without skipping any
  • Keep the operation O(1) extra space
  • Define the boundary (expires_at equal to now counts as expired)
Key Topics to Cover
linked lists
dummy head
in-place deletion
pointer manipulation
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
  • The list is sorted by expiry: how can you stop early?
  • Sessions also live in a hash map by id: how do you keep both structures consistent in O(1) per deletion?
  • Make the sweep safe while other threads insert at the head.
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
  • Each node has session_id, expires_at and next.
  • Remove nodes with expires_at <= now; keep the rest in order.
  • Mutate in place with O(1)O(1) extra memory and return the (possibly new) head....
High-Level Design

Insert a dummy node before the head and walk with a pointer prev that always points at the last kept node. Look at prev.next: if it is expired, splice it out by pointing prev.next past it and lo...


Submit Your Answer
Markdown supported