You’re on-call for a fintech fraud detection platform processing millions of card swipes per minute. A critical service keeps a fixed-size in-memory buffer of the most recent event IDs to power real-time alerting. During incident mitigation, you need to rotate this buffer efficiently to align “most recent” events with downstream consumers—without allocating extra memory that could increase GC pressure and tail latency.
Implement a function that rotates an integer array nums to the right by k steps.
nums directly) using O(1) extra space.nums: list[int] — array of event IDsk: int — number of steps to rotate rightInput: nums = [10, 20, 30, 40, 50, 60, 70], k = 3
Output: [50, 60, 70, 10, 20, 30, 40]
Explanation (step-by-step):
[50, 60, 70] move to the front.[10, 20, 30, 40] shifts right.Input: nums = [-1, -100, 3, 99], k = 2
Output: [3, 99, -1, -100]
Explanation:
[3, 99] wrap to the front.1 <= nums.length <= 2 * 10^5-10^9 <= nums[i] <= 10^90 <= k <= 10^9k is larger than len(nums), it should behave the same as rotating by k % len(nums).k == 0 or nums has length 1, the array should remain unchanged.n.