Design a Message Queue with Multi-Channel Messages and Channel-Based Subscriptions
Context
You are asked to design a message-queue data structure where:
-
Each message may belong to multiple communication channels (tags).
-
Consumers subscribe to channels and should receive messages if they share at least one channel with the message.
-
The system must support adding new channels dynamically, efficient detection of shared channels via union/intersection, and analysis of operation complexities.
-
Additionally, given a set of desired producer–consumer communication pairs, determine how many channels are needed and how to assign them to cover these patterns.
Requirements
-
Data model where messages can have multiple channels and consumers subscribe to channels.
-
Efficient operations:
-
Enqueue message
-
Dequeue/consume message
-
Subscribe/Unsubscribe consumer to channels
-
Query shared channels (e.g., does message M intersect consumer C’s subscriptions?)
-
Representation of channel identifiers enabling fast union/intersection.
-
Support adding channels at runtime.
-
Analyze time and space complexity for all operations.
-
Given a set of producer–consumer communication patterns, determine the minimum (or a good) number of channels required and how to assign them.