Problem, as an interviewer would state it:
"Rotate an array to the right by k positions. Discuss edge cases (negative k, k larger than the length), runtime, and how to do it in place."
Examples:
rotate([1, 2, 3, 4, 5, 6, 7], 3); // [5, 6, 7, 1, 2, 3, 4]
rotate([1, 2, 3, 4, 5], -2); // [3, 4, 5, 1, 2] (a negative k rotates left)
Clarifying questions expected:
- Rotate right or left, and how should a negative
kor aklarger than the length behave? - May I return a new array, or must the rotation happen in place with O(1) extra space?
- What about an empty array?
Code / implementation expected: Yes — real, direct proof against an independent reference, and a measurement of why "rotate one step, k times" is the wrong approach.