Bird
0
0

Consider this code snippet:

medium📝 Analysis Q5 of 15
LLD - Advanced LLD Concepts
Consider this code snippet:
lock.acquire()
shared_list.append(1)
lock.release()
print(len(shared_list))

What is the output if multiple threads run this code simultaneously with proper locking?
ALength may be incorrect due to race condition
BLength will always be 1
CLength will be equal to the number of threads
DCode will throw an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand locking effect

    Lock ensures only one thread appends at a time, preventing race conditions.
  2. Step 2: Predict output

    Each thread appends 1, so length equals number of threads.
  3. Final Answer:

    Length will be equal to the number of threads -> Option C
  4. Quick Check:

    Proper locking = correct list length [OK]
Quick Trick: Locks ensure correct shared list updates [OK]
Common Mistakes:
  • Ignoring lock effect
  • Assuming race condition corrupts length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes