Rotate a shift schedule
Python data engineering interview problem. Difficulty: beginner. Pattern: Sorting. About 10 minutes. Free to practice.
A store roster is a list of names in shift order. Operations sometimes rotate the list so yesterday's closer opens today, wrapping names from the end back to the front.
A team rotates its shift roster each week: the last people move to the front and everyone else keeps their order. Move the last k names to the front of the list and keep the rest in order. If k is larger than the list, wrap around. An empty list stays empty. Return a new list. Write rotate_roster(names, k). Rotating ['Ann','Bea','Cal'] by 1 puts Cal first: ['Cal','Ann','Bea']. Rotating ['Ann'] by 5 still gives ['Ann']. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Move the last k items to the front.
- Wrap around when k is bigger than the length.
- Return a new list and leave the input unchanged.
Constraints
- names is a list and can be empty.
- k is a whole number, 0 or more.
Examples
Input: rotate_roster(['Ann', 'Bea', 'Cal'], 1) Output: ['Cal', 'Ann', 'Bea'] This input follows the stated rules and produces this output.
Topics: lakebench, python, lists.
More Python interview questions · All interview problems · Learn data engineering
Rotate a shift schedule
Interview-style drill: Rotate a list of employee names by a given number of positions.
Rotate the shift list by `k` positions as specified, wrapping around the end.