Reverse a Packet Stream in Fixed-Size Groups of K Nodes

Last updated: November 29, 2026

Quick Overview

A singly linked list of network packets must be reversed in consecutive groups of k nodes; a final group with fewer than k nodes stays in its original order. Do it in place with O(1) extra space.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A singly linked list of network packets must be reversed in consecutive groups of k nodes; a final group with fewer than k nodes stays in its original order. Do it in place with O(1) extra space.

A well known Google onsite problem that stresses pointer bookkeeping across group boundaries. The interviewer is watching how you structure the code (helper for one group, a pointer to the previous group's tail) and whether you check that a full group exists before reversing.

What the Interviewer Expects
  • Check that k nodes remain before reversing a group
  • Reverse exactly k nodes and reconnect to the previous group's tail
  • Track the new tail of each reversed group for the next connection
  • Leave a short final group untouched
  • O(n) time and O(1) extra space, no recursion
Key Topics to Cover
linked list
group reversal
pointer bookkeeping
sentinel node
in-place
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
  • Reverse the short final group too.
  • Reverse alternate groups only.
  • Groups have variable sizes given by a list.
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 and k >= 1.
  • Output: head after reversing each full block of k nodes.
  • Nodes are relinked, not values swapped; O(1) extra space....
High-Level Design

Keep group_prev, the node right before the current group (initially a dummy). For each group: scan k nodes ahead from group_prev to find kth; if fewer than k remain, stop. Remember `group_next =...


Submit Your Answer
Markdown supported