KatInterview

Concurrency Safety, Deadlocks, and Array vs Linked List Tradeoffs

Data Structure DesignModel answer
MediumModel answerData Structure Designconcurrencythread-safetydeadlock

Last updated

Four short fundamentals questions: what makes code safe under concurrent use, what a deadlock is, how to handle deadlocks, and how contiguous arrays compare with pointer-based linked lists.

Imagine a quick oral round on core software engineering concepts. You get four short questions back to back. Answer each one precisely and briefly, the way you would say it aloud, and add a small concrete example wherever it sharpens the answer.

You may state your assumptions up front (for instance, language-neutral versus a specific language such as Java, threads in one process versus distributed locks, and how deep to go).

Question 1: Safe concurrent use. What does it mean for code to be thread-safe? What properties make a class or function safe to call from several threads at once, and what are the main techniques for achieving it?

Question 2: Deadlock. Define a deadlock. Give a minimal example, list the conditions that must all hold for one to occur, and explain how it differs from livelock and starvation.

Question 3: Dealing with deadlocks. What strategies exist for handling deadlocks? Cover prevention, avoidance, and detection with recovery, and mention practical habits for keeping them out of production code and for diagnosing one that has happened.

Question 4: Arrays versus linked lists. Compare the two in terms of memory layout, the cost of common operations (index access, search, insertion and deletion at the ends and in the middle, append on a growable array), cache behavior, and per-element overhead. Say when you would pick each one.

A strong answer uses exact definitions, shows a concrete example for each idea, names the cost of each remedy rather than just the remedy, and shows practical judgment.

Hints

Hint 1

For Q1, start from the failure: shared mutable state plus unsynchronized access gives lost updates (two threads doing count++). Then list the fixes (confinement, immutability, locks, atomics, concurrent collections) and what each costs.

Hint 2

For Q2 and Q3, tie everything to the four necessary conditions (mutual exclusion, hold and wait, no preemption, circular wait). Each prevention technique works by breaking exactly one of them.

Hint 3

For Q4, separate the asymptotic story from the hardware story: a linked list's O(1) insertion assumes you already hold the node, and finding it costs O(n) with poor cache locality, so arrays usually win in practice.