PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Implement memcpy with misaligned addresses

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of low-level memory manipulation, pointer alignment, and the implementation of efficient byte-copy routines in C.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Implement memcpy with misaligned addresses

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a C-style `memcpy` that correctly handles misaligned source/destination addresses. ## Task Write a function: ```c void* my_memcpy(void* dst, const void* src, size_t n); ``` It must copy exactly `n` bytes from `src` to `dst` and return `dst`. ## Requirements - Assume `src` and `dst` memory ranges **do not overlap** (i.e., `memcpy` semantics, not `memmove`). - `src` and/or `dst` may be **arbitrarily aligned** (not necessarily word-aligned). - Your implementation should be safe and correct for all `n >= 0`. - Optimize the copy by using wider loads/stores (e.g., `sizeof(size_t)` chunks) when possible, but still handle leading/trailing misaligned bytes correctly. ## Constraints / Notes - You may not call the standard library `memcpy`. - You may assume a typical flat address space and that unaligned access may be slower (or potentially illegal on some platforms), so alignment handling matters.

Quick Answer: This question evaluates a candidate's understanding of low-level memory manipulation, pointer alignment, and the implementation of efficient byte-copy routines in C.

Related Interview Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Implement Event Filtering and Queue Routing - Amazon (medium)
  • Determine if all courses can be completed - Amazon (medium)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
Amazon logo
Amazon
Jan 2, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
2
0
Loading...

Implement a C-style memcpy that correctly handles misaligned source/destination addresses.

Task

Write a function:

void* my_memcpy(void* dst, const void* src, size_t n);

It must copy exactly n bytes from src to dst and return dst.

Requirements

  • Assume src and dst memory ranges do not overlap (i.e., memcpy semantics, not memmove ).
  • src and/or dst may be arbitrarily aligned (not necessarily word-aligned).
  • Your implementation should be safe and correct for all n >= 0 .
  • Optimize the copy by using wider loads/stores (e.g., sizeof(size_t) chunks) when possible, but still handle leading/trailing misaligned bytes correctly.

Constraints / Notes

  • You may not call the standard library memcpy .
  • You may assume a typical flat address space and that unaligned access may be slower (or potentially illegal on some platforms), so alignment handling matters.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.