Bird
0
0

Examine this code snippet for a notification system:

medium📝 Analysis Q6 of 15
LLD - Design — Online Shopping Cart
Examine this code snippet for a notification system:
class Subject:
    def __init__(self):
        self.observers = set()
    def add_observer(self, observer):
        self.observers.add(observer)
    def notify_all(self):
        for observer in self.observers:
            observer.update()

What is the main issue that could cause notifications to fail?
AUsing a set for observers causes duplicate notifications
BObservers may not implement the update() method
Cnotify_all method should pass the subject as a parameter
Dadd_observer should use append instead of add
Step-by-Step Solution
Solution:
  1. Step 1: Analyze observer collection type

    Using a set is valid to avoid duplicates.
  2. Step 2: Check notify_all implementation

    notify_all calls observer.update() without parameters.
  3. Step 3: Identify potential failure

    If observers do not implement update(), calling update() will raise an error.
  4. Final Answer:

    Observers may not implement the update() method -> Option B
  5. Quick Check:

    Observer must implement update() [OK]
Quick Trick: Observers must implement update() method [OK]
Common Mistakes:
  • Assuming set causes duplicates
  • Forgetting to pass subject if required
  • Using append on a set collection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes