Reported August 2022
Skydioarray

Overlap-Safe Memory Copy

Reported by candidates from Skydio's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Skydio OA. Under 2s to a working solution.
Founder's read

The naive loop looks right and fails the moment the ranges overlap. That's the whole point of the Skydio OA question reported in August 2022, Overlap-Safe Memory Copy. You copy length bytes from sourceStart to destinationStart in an integer array, and the result has to match a copy through a temporary buffer. It's an array problem with one trap, and the trap is direction. If you're taking this OA in the next couple of days, learn the trap now. StealthCoder is there as a safety net on the live OA if you blank, but this one is small enough to own yourself.

The problem

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 = 0
destinationStart = 2
length = 3
return = [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 = 2
destinationStart = 0
length = 3
return = [3,4,5,4,5]

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

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is copy direction, which is exactly what memmove does. If destinationStart is greater than sourceStart and the ranges overlap, a forward loop overwrites source bytes before it reads them. Example 1 shows it: copying forward from index 0 to 2 would turn the buffer into [1,2,1,2,1]. So copy backward, from i = length-1 down to 0. Otherwise, copy forward. When destinationStart is less than sourceStart, forward is safe, as Example 2 shows. Equal starts mean nothing changes. The simplest safe alternative is slicing out the source range into a temp array first, then writing it. That's O(length) extra space, and it's fine for 100000 elements. The common pitfall is picking one direction for every case. Also check length = 0 and an empty memory array. If you blank during the live OA, StealthCoder can surface the direction check so you can sanity-test against both examples.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Overlap-Safe Memory Copy cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Skydio's OA.

Skydio reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Overlap-Safe Memory Copy FAQ

What's the trick in Overlap-Safe Memory Copy?+

Choose the copy direction based on overlap. If the destination starts after the source, copy from the end backward so you don't overwrite unread bytes. Otherwise copy forward. Or just snapshot the source slice first and write it out. Both give the same result as a temp buffer.

How hard is this Skydio OA question really?+

Easy on algorithm, easy to get wrong on edge cases. There's no clever data structure. The failure mode is a forward-only loop that passes Example 2 but breaks Example 1. If you test both examples by hand, you'll catch it.

Can I just use a temporary copy?+

Yes. The problem literally defines correctness as the temp buffer result. Slice memory from sourceStart to sourceStart plus length, then write it into the destination. It costs O(length) extra space, which is fine at 100000. The in-place backward loop is the cleaner answer if you're asked for O(1) space.

What edge cases should I test?+

Test length = 0, an empty memory array, sourceStart equal to destinationStart, full overlap in both directions, and ranges that touch the array end. Each of those should leave the array valid. Run both given examples before submitting.

How do I prepare for this in 48 hours?+

Write the backward and forward loops once from memory. Trace Example 1 on paper with the forward loop and watch it break. Then do the same with the fixed version. Twenty minutes covers it. Skim other array-mutation problems that involve in-place overwrites.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Skydio.

OA at Skydio?
Invisible during screen share
Get it