0
0
HLDsystem_design~10 mins

Pub/sub pattern in HLD - Interactive Code Practice

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

Complete the code to define the main component that receives messages from publishers.

HLD
class [1]:
    def __init__(self):
        self.subscribers = []

    def subscribe(self, subscriber):
        self.subscribers.append(subscriber)

    def publish(self, message):
        for subscriber in self.subscribers:
            subscriber.receive(message)
Drag options to blanks, or click blank then click option'
ABroker
BPublisher
CSubscriber
DListener
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the Broker with Publisher or Subscriber roles.
Using 'Listener' which is not a standard term in pub/sub.
2fill in blank
medium

Complete the code to define the method that subscribers use to receive messages.

HLD
class Subscriber:
    def receive(self, [1]):
        print(f"Received: {message}")
Drag options to blanks, or click blank then click option'
Amsg
Bmessage
Cdata
Devent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one used in the method body.
Choosing generic names that don't match the code.
3fill in blank
hard

Fix the error in the publish method to correctly send messages to subscribers.

HLD
def publish(self, message):
    for subscriber in self.subscribers:
        subscriber.[1](message)
Drag options to blanks, or click blank then click option'
Asend
Breceive
Cnotify
Dpublish
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a non-existent method like 'send' or 'notify' on subscribers.
Using 'publish' which is a publisher's method.
4fill in blank
hard

Fill both blanks to create a dictionary that maps topics to subscriber lists.

HLD
class Broker:
    def __init__(self):
        self.topics = [1]  # Initialize empty dictionary

    def subscribe(self, topic, subscriber):
        if topic not in self.topics:
            self.topics[topic] = [2]  # Initialize list for new topic
        self.topics[topic].append(subscriber)
Drag options to blanks, or click blank then click option'
A{}
B[]
Cset()
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a set instead of a list for subscribers.
Initializing topics as None or list instead of dictionary.
5fill in blank
hard

Fill all three blanks to implement publishing a message to all subscribers of a topic.

HLD
def publish(self, topic, message):
    subscribers = self.topics.get([1], [2])
    for subscriber in subscribers:
        subscriber.[3](message)
Drag options to blanks, or click blank then click option'
Atopic
B[]
Creceive
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong default value in get method.
Calling a wrong method on subscriber.