FastPrepOverlap-Safe Memory Copy

Overlap-Safe Memory Copy

Skydio logoSkydio● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given a byte buffer represented by an integer array memory. Copy length bytes starting at sourceStart into the range starting at destinationStart, then return the mutated buffer.

Source and destination ranges may overlap. The result must be the same as if the source bytes were first copied into a temporary buffer. Every array value is between 0 and 255.

Function

copyBytes(memory: int[], sourceStart: int, destinationStart: int, length: int) → int[]

Examples

Example 1

memory = [1,2,3,4,5]sourceStart = 0destinationStart = 2length = 3return = [1,2,1,2,3]

Backward copying preserves bytes 2 and 3 before they are overwritten.

Example 2

memory = [1,2,3,4,5]sourceStart = 2destinationStart = 0length = 3return = [3,4,5,4,5]

Constraints

  • 0 <= memory.length <= 100000.
  • Both ranges are within the array.
  • 0 <= length <= memory.length.

More Skydio problems

See Skydio hiring insights
public int[] copyBytes(int[] memory, int sourceStart, int destinationStart, int length) {
  // write your code here
}
memory[1,2,3,4,5]
sourceStart0
destinationStart2
length3
expected[1,2,1,2,3]
Checking account…