Overlap-Safe Memory Copy
Updated
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 interview questions
Similar questions at other companies
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Interval List IntersectionsGrammarly · PHONE SCREEN
- Subarray Product Less Than KApple · PHONE SCREENApple interview questions
- 3SumAmazon · PHONE SCREEN
- Subarray Product Less Than KAngel One · PHONE SCREEN
- Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions