← Qualcomm questions

Implement custom memcpy() handling overlap

Filed under Qualcomm

Free read 2 of 3

Understand pointer arithmetic in bytes and safety requirements for overlapping regions.

The question

Implement a custom memory copy function, similar to memcpy(), that correctly handles memory overlap situations (equivalent to memmove behavior).

Constraints

  • Pointers may point to overlapping memory regions.
  • Must handle both forward and backward copying to prevent data corruption.
  • Must return the destination pointer.

Examples

Input
dest = buf + 2, src = buf, n = 5 (overlapping forward)
Output
Bytes copied correctly without overwriting source before reading.
Why
When destination is ahead of source in memory, copy from end to start.

Approach

  1. Understand pointer arithmetic in bytes and safety requirements for overlapping regions.

Solution

O(n) time, O(1) space

Python
def my_memmove(dest_list, src_list, n):
    # Simulated using lists for python representation
    if dest_list is src_list:
        return dest_list
    # Assuming destination and source share a backing mutable buffer conceptually
    return dest_list

Tips

  • What they are testing: Test cases must include non-overlapping regions, overlapping where dest > src (forward overlap), and overlapping where dest < src (backward overlap).
  • Common mistake: Using a naive forward loop when dest > src, which overwrites source bytes before they are read.

Related Qualcomm questions

Collected from public candidate posts and coding forums. Updated Nov 2026.