In this interview round, you are asked several conceptual software-engineering questions. Answer all parts clearly and concisely, using examples.
1. Basic mutex programming
You have a multithreaded application where multiple threads increment a shared counter and occasionally read/write fields of a shared object.
-
What is a
mutex
and why is it needed in this scenario?
-
Describe how you would use a mutex to protect the shared counter from data races.
-
What kinds of bugs can occur if you update shared data from multiple threads
without
proper synchronization?
-
Briefly compare a mutex to other primitives such as a read–write lock or a semaphore.
2. Memory efficiency and alignment
-
What is
memory alignment
?
-
How can the layout/order of fields inside a struct/class affect memory efficiency because of alignment and padding?
-
Give a small example (e.g., a struct with several fields) and explain how reordering its fields could reduce memory usage.
-
Why might poor memory layout hurt performance (e.g., cache behavior)?
3. Polymorphism
-
What is
polymorphism
in object-oriented programming?
-
Explain the difference between
compile-time
(static) polymorphism and
run-time
(dynamic) polymorphism, with examples.
-
Give a concrete example with a base class and two derived classes where polymorphism makes the code more extensible or cleaner.
4. Idempotent operations
-
What does it mean for an operation to be
idempotent
?
-
Give at least two examples of idempotent operations in software systems (for example, in APIs, databases, or distributed systems).
-
Why is idempotency important in the context of retries, failures, or distributed systems?