Implement memmove
Updated
Problem statement
Implement memmove on a small simulated block of memory. The array memory holds one byte value per position. Copy the n bytes that start at position src into the n positions that start at position dest, then return memory.
The two ranges may overlap. The result must be the same as if the n source bytes were first saved somewhere else and then written to the destination. Every position outside the destination range keeps its value.
Follow-up: can you do it without a second buffer, using only a constant amount of extra space?
Function
memmove(memory: int[], dest: int, src: int, n: int) → int[]Examples
Example 1
memory = [1,2,3,4,5,6,7,8]dest = 2src = 0n = 4return = [1,2,1,2,3,4,7,8]The source is positions 0 to 3, holding 1, 2, 3, 4. The destination is positions 2 to 5, so the two ranges overlap. After the move, positions 2 to 5 must hold 1, 2, 3, 4, and every other position keeps its value, so the result is [1,2,1,2,3,4,7,8]. Copying front to back would be wrong here: it would overwrite positions 2 and 3 before reading them and give [1,2,1,2,1,2,7,8].
Example 2
memory = [1,2,3,4,5,6,7,8]dest = 0src = 3n = 4return = [4,5,6,7,5,6,7,8]The source is positions 3 to 6, holding 4, 5, 6, 7. They are written to positions 0 to 3. The ranges overlap at position 3. Positions 4 to 7 keep 5, 6, 7, 8, so the result is [4,5,6,7,5,6,7,8].
Example 3
memory = [9,8,7,6,5]dest = 3src = 0n = 2return = [9,8,7,9,8]The source is positions 0 and 1, holding 9, 8. They are written to positions 3 and 4. The ranges do not overlap, so the result is [9,8,7,9,8].
Constraints
1 <= memory.length <= 10000 <= memory[i] <= 2550 <= n <= memory.length0 <= srcandsrc + n <= memory.length0 <= destanddest + n <= memory.length
More NVIDIA Phone Screen questions
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
- Linked List Palindrome in Constant SpacePHONE SCREEN · Seen Aug 2026
- LRU Key-Value Cache OperationsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
- Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceGet My Parking · PHONE SCREEN