Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is concurrency in system design?
Concurrency means multiple tasks or processes are happening at the same time or overlapping in time to improve efficiency and performance.
Click to reveal answer
beginner
Why do we need locks or mutexes in concurrent systems?
Locks or mutexes prevent multiple processes from changing the same data at the same time, avoiding errors and data corruption.
Click to reveal answer
intermediate
What is a race condition?
A race condition happens when two or more processes try to change shared data at the same time, causing unpredictable results.
Click to reveal answer
intermediate
Explain deadlock in concurrency.
Deadlock is when two or more processes wait forever for each other to release resources, so none can continue.
Click to reveal answer
intermediate
What is the difference between concurrency and parallelism?
Concurrency is about managing multiple tasks at once, possibly by switching between them. Parallelism is running multiple tasks exactly at the same time using multiple processors.
Click to reveal answer
What problem does a mutex solve in concurrent systems?
AAutomatically fixes bugs in code
BIncreases the speed of a single process
CAllows unlimited access to shared resources
DPrevents multiple processes from accessing shared data simultaneously
✗ Incorrect
Mutexes ensure only one process accesses shared data at a time to avoid conflicts.
Which of the following best describes a race condition?
AProcesses waiting forever for each other
BMultiple processes accessing shared data causing unpredictable results
CA process running faster than expected
DA system crash due to hardware failure
✗ Incorrect
Race conditions occur when concurrent processes access shared data without proper synchronization.
Deadlock occurs when:
AA single process crashes
BProcesses run in parallel without issues
CProcesses wait forever for resources held by each other
DResources are never requested
✗ Incorrect
Deadlock is a state where processes wait endlessly for resources locked by each other.
Which technique helps avoid race conditions?
AUsing locks or synchronization mechanisms
BRunning processes sequentially only
CIgnoring shared data
DIncreasing CPU speed
✗ Incorrect
Locks and synchronization ensure safe access to shared data, preventing race conditions.
What is the main difference between concurrency and parallelism?
AConcurrency is multitasking; parallelism is multitasking on multiple processors
BConcurrency is faster than parallelism
CParallelism uses one processor only
DThey are the same
✗ Incorrect
Concurrency manages multiple tasks by switching; parallelism runs tasks simultaneously on multiple processors.
Describe common concurrency problems and how to handle them.
Think about what happens when multiple processes share data or resources.
You got /4 concepts.
Explain the difference between concurrency and parallelism with examples.
Consider how tasks are managed or executed at the same time.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using locks in concurrent systems?
easy
A. To allow unlimited access to shared resources
B. To prevent multiple threads from accessing shared data simultaneously
C. To speed up the execution of a single thread
D. To reduce memory usage in the system
Solution
Step 1: Understand concurrency risks
When multiple threads access shared data at the same time, it can cause errors or inconsistent results.
Step 2: Role of locks
Locks ensure only one thread accesses the shared data at a time, preventing conflicts and data corruption.
Final Answer:
To prevent multiple threads from accessing shared data simultaneously -> Option B
Quick Check:
Locks protect shared data = C [OK]
Hint: Locks protect shared data from simultaneous access [OK]
Common Mistakes:
Thinking locks speed up single-thread execution
Believing locks allow unlimited resource access
Confusing locks with memory optimization
2. Which of the following is the correct way to acquire a lock in a typical low-level design?
easy
A. lock.notify() before accessing shared data
B. lock.release() before accessing shared data
C. lock.wait() after accessing shared data
D. lock.acquire() before accessing shared data
Solution
Step 1: Understand lock usage order
To safely access shared data, a thread must first acquire the lock to block others.
Step 2: Correct method to acquire lock
The method lock.acquire() is used to obtain the lock before accessing shared data.
Final Answer:
lock.acquire() before accessing shared data -> Option D
Quick Check:
Acquire lock first = A [OK]
Hint: Acquire lock before shared data access [OK]
Common Mistakes:
Releasing lock before access
Using wait or notify incorrectly
Confusing acquire with release
3. Consider this pseudocode for two threads incrementing a shared counter without locks:
The code acquires a lock but never releases it, so other threads waiting for the lock will block forever.
Step 2: Understand deadlock impact
This causes a deadlock where threads cannot proceed, halting system progress.
Final Answer:
Deadlock due to missing lock release -> Option A
Quick Check:
Missing release causes deadlock = A [OK]
Hint: Always release locks after acquiring [OK]
Common Mistakes:
Thinking it's a syntax error
Assuming no issue without release
Confusing deadlock with data race
5. You design a system where multiple threads read and write a shared cache. To improve performance, you want to allow multiple readers but only one writer at a time. Which concurrency control mechanism fits best?
hard
A. Use a read-write lock allowing concurrent reads but exclusive writes
B. Use a simple mutex lock for all access
C. Use no locks and rely on thread scheduling
D. Use a semaphore with count 1 for all operations
Solution
Step 1: Understand concurrency needs for readers and writers
Multiple readers can safely access shared data simultaneously, but writers need exclusive access to avoid conflicts.
Step 2: Choose appropriate lock type
A read-write lock allows many readers at once but only one writer, balancing concurrency and safety efficiently.
Final Answer:
Use a read-write lock allowing concurrent reads but exclusive writes -> Option A
Quick Check:
Read-write lock fits multiple readers, single writer = B [OK]
Hint: Read-write locks allow many readers, one writer [OK]