84. Reverse Nodes in k-Group
hardAsked at SnowflakeReverse a linked list in groups of k nodes. Snowflake asks this because batched reversal in groups is precisely how their executor flips row direction in fixed-size batches for ORDER BY DESC over partial micro-partitions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake execution-engine team uses this for batch-flip discussion.
- LeetCode Discuss (2025-11)— Reported at Snowflake SDE-II screens.
Problem
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed.
Constraints
The number of nodes in the list is n.1 <= k <= n <= 50000 <= Node.val <= 1000
Examples
Example 1
head = [1,2,3,4,5], k = 2[2,1,4,3,5]Example 2
head = [1,2,3,4,5], k = 3[3,2,1,4,5]Approaches
1. Recursive group reverse
Check k nodes ahead. If they exist, reverse them, recursively process the rest.
- Time
- O(n)
- Space
- O(n/k) recursion
function reverseKGroup(head, k) {
let curr = head;
let count = 0;
while (curr && count < k) { curr = curr.next; count++; }
if (count < k) return head;
// reverse first k
let prev = reverseKGroup(curr, k);
let h = head;
for (let i = 0; i < k; i++) {
const next = h.next;
h.next = prev;
prev = h;
h = next;
}
return prev;
}Tradeoff: Recursive — stack depth is O(n/k). Clean but blows on adversarial n = 5000 and k = 1.
2. Iterative with group reverse (optimal)
Dummy head, prevTail walks forward. For each group of k: locate the end; reverse the segment in place; reattach.
- Time
- O(n)
- Space
- O(1)
function reverseKGroup(head, k) {
const dummy = { val: 0, next: head };
let prevTail = dummy;
while (true) {
let kth = prevTail;
for (let i = 0; i < k && kth; i++) kth = kth.next;
if (!kth) break;
const groupStart = prevTail.next;
const next = kth.next;
// Reverse [groupStart..kth]
let prev = next;
let curr = groupStart;
while (curr !== next) {
const nx = curr.next;
curr.next = prev;
prev = curr;
curr = nx;
}
prevTail.next = kth;
prevTail = groupStart;
}
return dummy.next;
}Tradeoff: Iterative, O(1) stack. The 'group-end probe before reverse' avoids reversing a partial group.
Snowflake-specific tips
Snowflake interviewers want the iterative version and the probe-before-reverse pattern. Bonus signal: connect to batch-flip during ORDER BY DESC — when their executor needs to reverse a column chunk for descending order, it flips in fixed-batch groups (vectorized for SIMD friendliness).
Common mistakes
- Reversing partial groups (must leave trailing < k nodes intact).
- Forgetting to reattach prevTail to the new group head AFTER reverse.
- Recursive version with k = 1 — stack depth equals n, which is too deep for n = 5000.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Swap Nodes in Pairs (LC 24) — k = 2 special case.
- Reverse alternate k groups.
- How does Snowflake batch-flip rows for descending order?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why probe before reversing?
If the remaining list is shorter than k, we must leave it alone. Probing first avoids accidentally reversing a partial group.
How does this connect to ORDER BY DESC?
For descending order over already-sorted data, the executor reverses chunks of rows. Doing it in fixed-size batches (instead of one giant reverse) keeps the working set in cache.
Practice these live with InterviewChamp.AI
Drill Reverse Nodes in k-Group and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →