← Questions

Mutexes, memory alignment, polymorphism and idempotency

0
MicrosoftCodingLast asked Oct 2025

A coding interview question asked in Microsoft's coding round, last asked Oct 2025. It tests concurrency, memory layout and object-oriented.

A round of short conceptual questions on software engineering fundamentals. Answer each clearly, with a small example, and expect to be pushed one level deeper.

Part 1: mutexes

Several threads increment a shared counter and sometimes read and write fields of a shared object.

  • What a mutex is and why this program needs one.
  • How you would protect the counter with a mutex; keep the critical section small and release the lock on every path (scoped locks).
  • What goes wrong without synchronization: lost updates, torn reads, reordering, inconsistent objects.
  • How a mutex compares with a read-write lock, a semaphore and an atomic counter, and when you would pick each.

Part 2: memory alignment and efficiency

  • What alignment is and why the hardware or compiler requires it.
  • How field order inside a struct adds padding. Example: on a typical 64-bit platform, struct { char a; double b; char c; } takes 24 bytes, while struct { double b; char a; char c; } takes 16.
  • Why poor layout also hurts speed: fewer useful bytes per cache line, false sharing between threads.

Part 3: polymorphism

  • What polymorphism is.
  • Compile-time polymorphism (overloading, templates or generics) against run-time polymorphism (virtual methods), with an example of each.
  • How virtual dispatch works underneath (a table of function pointers per class, a pointer to it in each object) and what it costs.
  • A base class with two derived classes where polymorphism makes the code easier to extend.

Part 4: idempotency

  • What makes an operation idempotent: doing it twice has the same effect as doing it once.
  • Examples: HTTP PUT and DELETE, setting a value as opposed to incrementing it, an insert keyed by a client-chosen id.
  • Why it matters for retries in distributed systems, and how idempotency keys make a non-idempotent operation such as a payment safe to retry.

What a strong answer covers

  • Correct definitions in plain words, each backed by a concrete example.
  • Trade-offs, not only facts: lock granularity against contention, packing against alignment, virtual calls against templates.
  • Links between the topics, such as idempotent handlers and retries after a timeout.

Follow-ups

  • What is a deadlock, and two ways to prevent one?
  • When is volatile not enough for thread safety?
  • How do you store idempotency keys, and for how long?

Where it's asked

  • Microsoft · Coding · Last asked Oct 202560 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent

What it tests

concurrency · memory layout · object-oriented

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding