← Amplitude Interview Insights

Amplitude·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Amplitude SWE interview with a linked list problem that looks simple on paper but has enough pointer edge cases to trip you up if you're not careful. No frills, just code.

Questions Asked (1)

Q1

Given the head of a singly linked list and an integer K, swap the K-th node from the end with the head node in-place (O(1) extra memory) and return the new head.

Algorithms & Data Structures
Author's notes

The base cases are where this gets you.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the two-pointer technique to find the K-th node from the end in one pass, then swap it with the head by adjusting pointers. Handle edge cases such as K=1 (swap head with itself) and K equal to the list length (swap head with tail).

Pro tip: Clarify whether K is 1-indexed from the end and whether swapping with the head when K=1 should be a no-op. Also, consider if K is greater than the list length—return the original list or handle as an error.

1. Understand the problem and edge cases

Confirm that K is 1-indexed from the end, and identify edge cases: empty list, K=1, K=list length, and K > list length.

2. Find the K-th node from the end

Use two pointers: move the first pointer K nodes ahead, then move both until the first reaches the end. The second pointer will be at the K-th node from the end.

3. Identify the previous node of the target

While moving the second pointer, also keep track of the node before it (prev) to facilitate pointer updates during swap.

4. Swap the head and target node

Adjust pointers: set prev.next to head, target.next to the original head's next, and update the new head to be the target node. Handle special cases where target is the head or adjacent to head.

5. Return the new head

After swapping, return the target node as the new head of the list.

Key Points to Mention

  • Two-pointer technique for finding K-th from end in one pass
  • O(1) extra space complexity
  • Handling edge cases: K=1, K=length, K>length, empty list
  • Pointer manipulation details to avoid cycles or broken links
  • Time complexity O(n) and space complexity O(1)
  • Clarifying assumptions about K (1-indexed, valid range)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.