Reverse a Watch History Linked List in Groups of K

Last updated: November 29, 2026

Quick Overview

A device stores watch history as a singly linked list. Reverse the nodes in consecutive groups of k in place, leaving a final group with fewer than k nodes in its original order, using O(1) extra memory.

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

0

0

0 solved


A device stores watch history as a singly linked list. Reverse the nodes in consecutive groups of k in place, leaving a final group with fewer than k nodes in its original order, using O(1) extra memory.

Pointer manipulation questions are still common in TikTok onsite coding, and this one tests whether you can reverse sublists in place without losing track of group boundaries. The interviewer watches for a dummy head, a clean check that k nodes remain before reversing, and correct reconnection of each group.

What the Interviewer Expects
  • Uses a dummy node so the first group is not a special case
  • Checks that a full group of k exists before modifying anything
  • Reverses the group in place with constant extra pointers
  • Reconnects the previous group's tail and the next group's head correctly
  • Tests k = 1, k larger than the list, and an exact multiple
Key Topics to Cover
linked lists
in-place reversal
pointer manipulation
dummy head pattern
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
  • Do it recursively and compare stack usage.
  • Reverse the trailing partial group too.
  • Reverse only every other group of k.
  • How would you test pointer code like this systematically?
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

Singly linked list, positive k. Groups of exactly k are reversed; the final group shorter than k stays as is. In place, O(1) extra memory, return the new head....

High-Level Design

Keep group_prev, the node right before the current group (initially a dummy pointing at head). Walk k steps from it to find kth; if we hit None first, we are done. Remember group_next = kth.next...


Submit Your Answer
Markdown supported