0
0
LLDsystem_design~10 mins

Thread safety in design in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a thread-safe counter using a lock.

LLD
class Counter:
    def __init__(self):
        self.value = 0
        self.lock = [1]

    def increment(self):
        with self.lock:
            self.value += 1
Drag options to blanks, or click blank then click option'
Athreading.Event()
Bthreading.Condition()
Cthreading.Thread()
Dthreading.Lock()
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread instead of Lock causes errors because Thread is for creating threads, not synchronization.
Using Event or Condition without proper locking does not guarantee exclusive access.
2fill in blank
medium

Complete the code to safely read a shared variable with a read-write lock.

LLD
class SharedData:
    def __init__(self):
        self.data = 0
        self.rw_lock = [1]

    def read(self):
        self.rw_lock.acquire_read()
        value = self.data
        self.rw_lock.release_read()
        return value
Drag options to blanks, or click blank then click option'
Athreading.Semaphore()
BReadWriteLock()
Cthreading.Lock()
Dthreading.RLock()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a simple Lock blocks all threads, reducing concurrency.
RLock allows reentrant locking but does not differentiate read/write access.
3fill in blank
hard

Fix the error in the code to avoid race conditions when updating a shared list.

LLD
shared_list = []

lock = threading.Lock()

def add_item(item):
    [1]  # Fix: ensure thread safety
    shared_list.append(item)
    lock.release()
Drag options to blanks, or click blank then click option'
Alock.acquire()
Block.release()
Clock.wait()
Dlock.notify()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling release before acquire causes runtime errors.
Using wait or notify without proper locking does not prevent race conditions.
4fill in blank
hard

Fill both blanks to implement a thread-safe singleton pattern.

LLD
class Singleton:
    _instance = None
    _lock = [1]

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    cls._instance = [2]  # Create instance
        return cls._instance
Drag options to blanks, or click blank then click option'
Athreading.Lock()
Bthreading.Thread()
CSingleton()
Dobject()
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread instead of Lock causes synchronization issues.
Creating the instance with object() does not call the Singleton constructor.
5fill in blank
hard

Fill all three blanks to implement a thread-safe producer-consumer queue.

LLD
import queue
import threading

class ProducerConsumer:
    def __init__(self):
        self.q = queue.Queue()
        self.lock = [1]
        self.not_empty = threading.Condition(self.lock)

    def produce(self, item):
        with self.lock:
            self.q.put(item)
            [2]  # Notify consumers

    def consume(self):
        with self.lock:
            while self.q.empty():
                [3]  # Wait for items
            return self.q.get()
Drag options to blanks, or click blank then click option'
Athreading.Lock()
Bself.not_empty.notify()
Cself.not_empty.wait()
Dthreading.Event()
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a lock causes race conditions.
Calling notify without holding the lock causes errors.
Waiting without a loop can cause missed signals.