DSA interview question
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.
Node* reverseKGroup(Node* head, int k); // k >= 1; returns the new head
Input (list, k) | Output list | Explanation |
|---|---|---|
(1 -> 2 -> 3 -> 4 -> 5, 2) | 2 -> 1 -> 4 -> 3 -> 5 | The 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 -> 4 | Two full groups |
(1 -> 2, 1) | 1 -> 2 | k = 1 changes nothing |
(1, 2) | 1 | Fewer 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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in