Remove the Nth Node from the End
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mygate reportedly put "Remove the Nth Node from the End" in front of candidates in September 2023, and the example where a single-node list returns empty is the detail that trips people up. It's a linked list problem, not a graph problem, whatever the tag says. The pattern is two pointers with a gap, and you can solve it in one pass. If you've got an OA coming in a day or two, this is a good one to lock down. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment.
The problem
Given the head of a singly linked list and an integer n, remove the nth node from the end and return the resulting head. Count from the end starting at 1: n = 1 removes the tail. The input is nonempty and acyclic, and n is valid for its length. Reuse the remaining nodes and preserve their order. Exactly one node is removed even when values repeat. Removing the only node returns an empty list. Lists are displayed as arrays of node values. Function removeNthFromEnd(head: ListNode, n: int) → ListNode Examples Example 1 head = [4,7,2,9,6] n = 2 return = [4,7,2,6] From the end, 6 is first and 9 is second, so the node with value 9 is removed. Example 2 head = [8] n = 1 return = [] The only node is also the first from the end. The resulting head is null. Constraints The list length L satisfies 1 <= L <= 1000. 1 <= n <= L. Each node value is an integer in [-10^6, 10^6]. The list contains no cycle.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a dummy node plus two pointers. Put a dummy node before the head. Start both pointers there. Move the fast pointer n steps ahead, then move both until fast reaches the last node. Now slow sits right before the node to delete, so set slow.next = slow.next.next. Return dummy.next. The dummy is what handles the single-node case from Example 2, where removing the head would otherwise need a special branch. The common pitfall is an off-by-one on the gap, which leaves you deleting the wrong node or crashing on n equal to the list length. Another is counting with a first pass, then forgetting that removing the head changes the return value. Values can repeat, so never search by value. Work by position only. If your mind goes blank mid-assessment, StealthCoder can hand you the pointer setup in real time.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Remove the Nth Node from the End cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as remove nth node from end of list. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Remove the Nth Node from the End FAQ
How hard is Remove the Nth Node from the End really?+
It's easy to medium. The logic is short, but the edge cases bite. Removing the head and the single-node list are where most wrong answers come from. A dummy node fixes both. If you can write it in under ten minutes, you're in good shape for the Mygate OA.
What's the trick to solving it in one pass?+
Use two pointers with a gap of n. Advance fast n nodes from a dummy head, then move both pointers until fast hits the last node. Slow then stops right before the target. Skip the target by relinking slow.next to slow.next.next. No length counting needed.
Do I need a dummy node?+
Not strictly, but use one. Without it you need a separate check for when n equals the list length, since that removes the head. The dummy makes every deletion look the same and returns null naturally when the only node is removed.
Does the two-pass approach pass?+
Yes. With length up to 1000, counting the length first and then walking to position L - n works fine. It's O(L) time and O(1) space. The one-pass version is neater and interviewers like it, but correctness matters more than elegance here.
How do I prep for this in 48 hours?+
Write it from scratch three times on a blank editor. Test n = 1, n = L, and a single-node list each time. Then try a variant, like removing the middle node. Duplicate values shouldn't matter since you delete by position, so confirm your code never compares values.