What is a race condition and how do you fix it (3 ways)?
The essence
A race condition is when the program's result depends on the order in which threads reach shared data, and that order is not controlled. The special and most dangerous case is a data race: two threads access the same memory at the same time, at least one writes, and there is no synchronization. A data race is undefined behavior (corrupted structures, crashes). A race condition can exist without a data race: for example check-then-act ("checked the key is absent, inserted" — but someone inserted between the check and the insert).
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep