Reverse Each Block of K Messages in a Linked Chat History
Last updated: November 29, 2026
Quick Overview
Given the head of a singly linked list of chat messages and an integer k, reverse the nodes in every consecutive block of k, leaving a final block shorter than k in its original order. Use O(1) extra space.
Meta
November 29, 20260
0
0 solved
Given the head of a singly linked list of chat messages and an integer k, reverse the nodes in every consecutive block of k, leaving a final block shorter than k in its original order. Use O(1) extra space.
A Meta onsite classic for linked list fluency, framed as reordering batches of Messenger messages for a display experiment. Interviewers watch pointer bookkeeping closely: a dummy head, reconnecting each reversed block to the previous one, and not reversing the short tail.
What the Interviewer Expects
- Use a dummy node so the first block needs no special case
- Check that k nodes remain before reversing a block
- Reverse the block in place and reconnect both ends
- Advance the 'previous tail' pointer correctly
- Achieve O(n) time and O(1) extra space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Reverse the trailing short block as well.
- Reverse alternate blocks only (reverse, skip, reverse).
- Do it recursively and discuss stack depth for long histories.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: head of a singly linked list of n nodes, and 1 <= k. Output: head of the list after reversing each full group of k nodes. Node values must not be swapped; relink the nodes themselves. Extra...
High-Level Design
Keep group_prev, the node just before the current group (initially a dummy pointing at head). From it, walk k steps to find the group's last node kth; if fewer than k nodes remain, stop. Remember ...