Rotating Chore Chart
Your shared house has a chore chart: a fixed list of chores and one name beside each. Every week the names move down one place, and whoever falls off the bottom wraps to the top.
After a holiday the chart is several weeks behind. You want to move the names down that many places in one go, rewriting the list you have rather than making a new one.
Your task
Write rotateChores(names, k). Move every name k places towards the end, wrapping around: the name at index i ends up at index (i + k) % names.length.
Change names in place and return nothing. k can be larger than the list, and the list can be empty. Try to use only O(1) extra memory.
rotateChores(names: string[], k: int) → void (changes names in place)
Examples
names = ["Ana", "Ben", "Caro", "Dev", "Eli"] k = 2
["Dev", "Eli", "Ana", "Ben", "Caro"]
Everyone moves down 2 places. Dev and Eli wrap to the top, in the same order.
names = ["Ana", "Ben", "Caro"] k = 7
["Caro", "Ana", "Ben"]
Moving 3 places brings everyone back, so 7 places is the same as 7 % 3 = 1 place. Caro wraps to the top.
Constraints
- 0 ≤ names.length ≤ 100,000
- 0 ≤ k ≤ 1,000,000,000
- Names are non-empty strings and may repeat.
Start with brute force
Rotate by one place, k times, by moving the last name to the front and shifting everyone else down.
Each one-place rotation moves all n names, so this costs n × k steps, and with 100,000 names and k in the billions it never finishes. Reducing k to k % n still leaves up to n² moves. Copying into a new list takes O(n) time but needs a second list of n names.
Hints
0 of 4 shown- Hint 1
Rotating
[A, B, C, D, E]by 2 gives[D, E, A, B, C]. Which block moved to the front, and did the order inside each block change? - Hint 2
Two blocks,
[A, B, C]and[D, E], swap places, keeping their inner order. Rotating by the list length changes nothing, so first setktok % n. - Hint 3
Reverse the whole list:
[E, D, C, B, A]. The blocks are in the right places, but each is backwards. What fixes that? - Hint 4
With
k = k % n: reverse the whole list, then the firstkitems, then the remainingn - k. One helper reverses a range in place with two pointers swapping inward.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Rotating right by k moves the last k names in front of the others, each block keeping its order. Reversing the whole list puts both blocks in place but flips each one. Reversing each block flips it back.
Trace [Ana, Ben, Caro, Dev, Eli] with k = 2:
- Since
k % 5 = 2, the value ofkstays 2. - Reversing the whole list gives
[Eli, Dev, Caro, Ben, Ana]. Dev and Eli are now at the top, but in backwards order. - Reversing the first 2 names gives
[Dev, Eli, Caro, Ben, Ana]. - Reversing the last 3 names gives
[Dev, Eli, Ana, Ben, Caro], which is the final result.
There are two traps to avoid.
- Reduce
kfirst, but check for an empty list before that, becausek % 0gives NaN in JavaScript and raises an error in Python. - "In place" means changing the list you were given.
names = newListonly repoints your local variable. In Python,names[:] = names[-k:] + names[:-k]does write into the original, but builds a full copy first.
function rotateChores(names, k) {
const n = names.length;
if (n === 0) return;
k = k % n; // full laps change nothing
reverse(names, 0, n - 1); // blocks swap places, but each is backwards
reverse(names, 0, k - 1); // fix the block that wrapped to the top
reverse(names, k, n - 1); // fix the rest
}
function reverse(a, lo, hi) {
while (lo < hi) {
[a[lo], a[hi]] = [a[hi], a[lo]];
lo++;
hi--;
}
}def rotate_chores(names, k):
n = len(names)
if n == 0:
return
k %= n # full laps change nothing
reverse(names, 0, n - 1) # blocks swap places, but each is backwards
reverse(names, 0, k - 1) # fix the block that wrapped to the top
reverse(names, k, n - 1) # fix the rest
def reverse(a, lo, hi):
while lo < hi:
a[lo], a[hi] = a[hi], a[lo]
lo += 1
hi -= 1Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.