Rotate a Linked Carousel of Stories Right by K Positions

Last updated: November 29, 2026

Quick Overview

Given the head of a singly linked list of stories and a non-negative k (possibly much larger than the length), rotate the list to the right by k places and return the new head.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the head of a singly linked list of stories and a non-negative k (possibly much larger than the length), rotate the list to the right by k places and return the new head.

A quick Meta phone-screen question framed around rotating the starting point of a Stories carousel. The interviewer checks whether you reduce k modulo the length, avoid an O(nk) step-by-step rotation, and reconnect the list with a single cut.

What the Interviewer Expects
  • Compute the length and the tail in one pass
  • Reduce k modulo the length
  • Close the list into a ring and cut at the right spot
  • Handle k = 0 and k a multiple of the length
  • State O(n) time
Key Topics to Cover
Linked list
Modular arithmetic
Rotation
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
  • Rotate left by k instead.
  • Rotate in place when you are given only a pointer to the tail of a circular list.
  • What if k can be negative?
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 of a list of length n >= 0 and k >= 0, which may be up to 2 * 10^9. Output: head after moving the last k mod n nodes to the front in order. Reuse nodes; O(1) extra space....

High-Level Design

Walk to the tail while counting n. If n is 0 or k % n is 0, return head unchanged. Otherwise connect tail.next = head to form a ring. The new tail is the node at index n - k % n - 1 from the old...


Submit Your Answer
Markdown supported