KatInterview

Concurrency, Struct Layout, Polymorphism, and Idempotency Explained

System DesignModel answer
MediumModel answerSystem Designconcurrencymemory-layoutobject-oriented-programming

Last updated

Answer four core software engineering questions covering thread synchronization, memory padding, OOP polymorphism, and idempotent operations.

Four Pillars: Concurrency, Memory, Polymorphism, Idempotency

You are given four short conceptual topics that frequently come up in software engineering interviews. For each one, give a clear definition, a concrete example, and at least one insight that goes beyond the surface.


Part 1 – Thread Synchronization with Locks

A shared counter is incremented by several threads simultaneously, and a shared object is read and written by those same threads.

  1. What is a mutex, and why can't you just let threads update the counter freely?
  2. Show, with pseudocode or real code, how you would guard the counter correctly.
  3. Name at least three distinct bugs that can appear when threads write shared data without any synchronization.
  4. How does a mutex differ from a read–write lock and a semaphore? When would you reach for each?

Part 2 – Struct Field Layout and Memory Padding

  1. What is memory alignment, and why do processors care about it?
  2. Explain how the order in which you declare fields in a struct or class affects how much memory the struct actually uses.
  3. Give a concrete struct example — at least three fields of different types — and show how reordering the fields reduces its size.
  4. Beyond raw size, how does a wasteful layout hurt runtime performance?

Part 3 – Polymorphism in Object-Oriented Design

  1. Define polymorphism in your own words.
  2. Contrast compile-time polymorphism with run-time polymorphism: how does each work, and what is the cost/benefit trade-off?
  3. Write a small example with a base type and two concrete implementations where polymorphism removes a type-checking conditional and makes the code easier to extend.

Part 4 – Idempotent Operations

  1. What does idempotency mean for an operation or API endpoint?
  2. Give at least two distinct examples from real systems — APIs, databases, or distributed systems.
  3. Why does idempotency matter so much when retries or partial failures are involved?

What is expected: crisp definitions, working examples, correct vocabulary, and at least one non-obvious point per section that signals genuine depth.

Hints

Hint 1

For Part 1, think about what 'counter += 1' actually compiles down to at the instruction level — it is not a single atomic step, and that gap is exactly where races live. Also ask yourself whether reads need the lock too.

Hint 2

For Part 2, work out the layout by hand: note each field's size and alignment requirement, then calculate how many padding bytes the compiler inserts between fields. The rule of thumb is to declare fields from largest to smallest alignment.

Hint 3

For Part 4, the key distinction is between an operation being safe to retry versus one that produces the same *outcome* no matter how many times it runs — think about PUT vs POST in HTTP, or why database upserts are designed the way they are.