Drop the Nth Most Recent Edit From a Linked Undo History

Last updated: November 29, 2026

Quick Overview

A document editor stores its edit history as a singly linked list from oldest to newest. Remove the nth edit counting from the newest end in one pass and return the new head.

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

0

0

0 solved


A document editor stores its edit history as a singly linked list from oldest to newest. Remove the nth edit counting from the newest end in one pass and return the new head.

A Microsoft phone screen favorite, phrased around undo stacks in Word or Visual Studio Code. The interviewer checks two pointer technique, a dummy head to avoid special casing the first node, and careful bounds handling.

What the Interviewer Expects
  • Use a lead pointer n steps ahead of a trail pointer
  • Use a dummy node so removing the head needs no special case
  • Complete the removal in one pass
  • Handle n larger than the list length
  • Talk through pointer positions on a short example
Key Topics to Cover
linked lists
two pointers
dummy node
one 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 do this on a doubly linked list?
  • What if the list is too large to traverse twice but you also need its length?
  • How would you remove every nth node from the end instead of one?
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 and integer n >= 1.
  • Remove the node that is n positions from the tail (n = 1 is the tail).
  • Output: the head after removal.
  • Clarify: if n exceeds the length,...
High-Level Design

Place a dummy node before the head. Advance lead n steps from the dummy; if it falls off the list, n is too large. Then move lead and trail together until lead is on the last node. Because the...


Submit Your Answer
Markdown supported