Rotate Array
Real-world version: Delay the Sample BufferThe statement here is our own: the same inputs, outputs and technique.
A firmware ring buffer holds one signed sensor delta per slot in the integer array readings. Slot 0 is the oldest slot and the buffer wraps, so the slot after the last one is slot 0 again.
A technician wants the whole trace pushed later by delay slots: whatever sits in a slot ends up delay slots further along, and anything that runs off the end reappears at the front. The value of delay can be 0, and it can also be larger than the number of slots, in which case the trace simply wraps more than once.
Rearrange readings itself, using no more than a constant amount of extra space on top of the buffer, then return readings.
Examples
Example 1
- Input
- readings = [5, -2, 9, 4], delay = 1
- Output
- [4, 5, -2, 9]
Every delta moves one slot later and the 4 that fell off the end lands in slot 0.
Example 2
- Input
- readings = [7, 8, -3], delay = 5
- Output
- [8, -3, 7]
Five pushes across three slots is one full wrap plus two more, so the outcome matches a push of two.
Example 3
- Input
- readings = [6, 1, 4], delay = 0
- Output
- [6, 1, 4]
Nothing is pushed, so the buffer comes back exactly as it arrived.
Constraints
- 1 <= readings.length <= 10^5
- -2^31 <= readings[i] <= 2^31 - 1
- 0 <= delay <= 10^5
- Only a constant amount of extra space may be used
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def delay_buffer(readings: list[int], delay: int) -> list[int]:public int[] delayBuffer(int[] readings, int delay)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Sort Transformed ArrayReal-world version: Sorted Dial ResponsesMedium
- Sum of GCD of Formed PairsReal-world version: Total Mesh Cycle of the Gear BinMedium
- Minimum Operations to Transform Array into Alternating PrimeReal-world version: Nudging Readings onto Prime ValuesMedium
- Lexicographically Smallest Negated Permutation that Sums to TargetReal-world version: Turning Numbers Over to Hit a TotalMedium