All problems
0034MediumArrayMathTwo Pointers

Delay the Sample Buffer

Tracked in this browser only
Write code

Trains the technique from

LeetCode 189Rotate Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def delay_buffer(readings: list[int], delay: int) -> list[int]:
Java
public int[] delayBuffer(int[] readings, int delay)
September 7
Apply