PPractick

DSA interview question

Reverse Nodes in k-Group

Hard · Data structures · Topics: linkedlist, recursion

The problem

A video player buffers frames in a singly linked list and, for a special "rewind in chunks" effect, needs to reverse the frames in consecutive groups of k. If fewer than k frames remain at the end, they stay in their original order. Reversing one group is the standard three-pointer reversal; the real challenge is stitching each reversed group back between the previous group's end and the rest of the list, without losing any node.

Example: 1 -> 2 -> 3 -> 4 -> 5, k = 2 → 2 -> 1 -> 4 -> 3 -> 5.

Function signature

Node* reverseKGroup(Node* head, int k);  // k >= 1; returns the new head

Test cases

Input (list, k)Output listExplanation
(1 -> 2 -> 3 -> 4 -> 5, 2)2 -> 1 -> 4 -> 3 -> 5The last node is a partial group
(1 -> 2 -> 3 -> 4 -> 5, 3)3 -> 2 -> 1 -> 4 -> 5
(1 -> 2 -> 3 -> 4 -> 5 -> 6, 3)3 -> 2 -> 1 -> 6 -> 5 -> 4Two full groups
(1 -> 2, 1)1 -> 2k = 1 changes nothing
(1, 2)1Fewer than k nodes

(Confirm: only node links change, not values; a final group shorter than k is left as is; O(1) extra memory is expected.)

How interviewers phrase it

Clarifying questions to ask

  1. What happens to a final group shorter than k? (Here it stays as is.)
  2. Can I change values, or only links? (Only links.)
  3. Is O(1) extra space required? (Rules out recursion if strict.)

Follow-ups to expect

  1. "How do you know a full group exists?"
  2. "How do you reconnect a reversed group?"
  3. "Why a dummy node?"
  4. "Recursive version?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in