Bird
Raised Fist0
LLDsystem_design~20 mins

Concurrency considerations in LLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Concurrency Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding race conditions in concurrent systems

In a system where multiple threads update a shared counter without synchronization, what is the most likely outcome?

AThe counter always increments correctly without any lost updates.
BThe system throws a runtime error when multiple threads access the counter.
CThe counter may have incorrect values due to lost updates caused by race conditions.
DThe counter resets to zero automatically after each update.
Attempts:
2 left
💡 Hint

Think about what happens when two threads try to update the same value at the same time.

Architecture
intermediate
2:00remaining
Choosing synchronization mechanisms for shared resources

You have a shared resource accessed by multiple threads. Which synchronization mechanism is best to ensure only one thread accesses it at a time?

AUse a mutex (lock) to allow only one thread to access the resource at a time.
BUse a thread pool to manage multiple threads accessing the resource simultaneously.
CUse a queue to store all requests and process them in parallel without locks.
DUse a timer to delay access to the resource by each thread.
Attempts:
2 left
💡 Hint

Think about how to prevent multiple threads from entering a critical section simultaneously.

scaling
advanced
2:00remaining
Scaling a system with concurrent database writes

You design a system where many users write data concurrently to a database. What is a common strategy to handle high write loads without causing contention?

AUse a single-threaded application server to queue all writes sequentially.
BUse a single database server with a global lock to serialize all writes.
CDisable transactions to speed up writes and avoid locking overhead.
DUse database sharding to split data across multiple servers, reducing contention on any single server.
Attempts:
2 left
💡 Hint

Think about dividing the workload to avoid bottlenecks on one server.

tradeoff
advanced
2:00remaining
Tradeoffs between optimistic and pessimistic concurrency control

Which statement best describes a tradeoff between optimistic and pessimistic concurrency control?

AOptimistic control is slower because it always locks; pessimistic control is faster because it assumes no conflicts.
BOptimistic control assumes conflicts are rare and retries on conflict; pessimistic control locks resources to prevent conflicts but can reduce concurrency.
COptimistic control locks resources before access; pessimistic control never uses locks and always retries.
DBoth optimistic and pessimistic control always lock resources but differ in lock duration.
Attempts:
2 left
💡 Hint

Consider how each method handles conflicts and resource locking.

estimation
expert
2:00remaining
Estimating throughput for a concurrent system with locks

A system has 10 threads trying to access a shared resource protected by a lock. Each thread holds the lock for 5 milliseconds per operation. Assuming no other delays, what is the maximum number of operations the system can perform per second?

A200 operations per second
B100 operations per second
C500 operations per second
D50 operations per second
Attempts:
2 left
💡 Hint

Calculate how many operations fit in one second considering the lock is held exclusively.

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

  1. Step 1: Understand concurrency risks

    When multiple threads access shared data at the same time, it can cause errors or inconsistent results.
  2. Step 2: Role of locks

    Locks ensure only one thread accesses the shared data at a time, preventing conflicts and data corruption.
  3. Final Answer:

    To prevent multiple threads from accessing shared data simultaneously -> Option B
  4. 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

  1. Step 1: Understand lock usage order

    To safely access shared data, a thread must first acquire the lock to block others.
  2. Step 2: Correct method to acquire lock

    The method lock.acquire() is used to obtain the lock before accessing shared data.
  3. Final Answer:

    lock.acquire() before accessing shared data -> Option D
  4. 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:
Thread 1: temp = counter
          temp = temp + 1
          counter = temp

Thread 2: temp = counter
          temp = temp + 1
          counter = temp
What is the possible final value of counter if it starts at 0?
medium
A. 2
B. Any negative number
C. 1
D. 0

Solution

  1. Step 1: Analyze concurrent increments without locks

    Both threads read the same initial value 0, increment it to 1, and write back 1, causing one increment to be lost.
  2. Step 2: Determine final counter value

    Because of race condition, the counter may only increase once, resulting in final value 1 instead of 2.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    Race condition causes lost update = 1 [OK]
Hint: Without locks, increments can overwrite each other [OK]
Common Mistakes:
  • Assuming both increments always succeed
  • Ignoring race conditions
  • Thinking counter can be negative here
4. In the following code snippet, what is the main concurrency issue?
lock.acquire()
shared_data.append(1)
# Missing lock.release()
medium
A. Deadlock due to missing lock release
B. Data race on shared_data
C. Syntax error in lock usage
D. No issue, code is safe

Solution

  1. Step 1: Identify missing lock release

    The code acquires a lock but never releases it, so other threads waiting for the lock will block forever.
  2. Step 2: Understand deadlock impact

    This causes a deadlock where threads cannot proceed, halting system progress.
  3. Final Answer:

    Deadlock due to missing lock release -> Option A
  4. 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

  1. 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.
  2. Step 2: Choose appropriate lock type

    A read-write lock allows many readers at once but only one writer, balancing concurrency and safety efficiently.
  3. Final Answer:

    Use a read-write lock allowing concurrent reads but exclusive writes -> Option A
  4. Quick Check:

    Read-write lock fits multiple readers, single writer = B [OK]
Hint: Read-write locks allow many readers, one writer [OK]
Common Mistakes:
  • Using simple mutex reduces concurrency
  • Ignoring need for exclusive write access
  • Relying on no locks causes data races