KatInterview

Simulate a Topic Message Broker with Subscription Tokens

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdesignhash-mapsimulation

Last updated

Simulate a topic-based message broker from a command log, handling token issuance, ordered delivery records, and idempotent unsubscribes.

You are building a small in-memory message broker and must replay a log of commands against it. Each command either registers a listener on a named topic, broadcasts a message to a topic, or cancels an earlier registration.

You are given two lists of equal length:

  • operations: a list of command names.
  • arguments: a list where arguments[i] holds the arguments for operations[i].

Process the commands in order, starting with an empty broker, and return one result per command.

Commands

"subscribe" with arguments [topic, listener_id]

  • Register listener_id on topic.
  • Every registration gets a new integer token. The first token issued is 1, and each later subscribe command gets the next integer (2, 3, ...), whatever topic it targets.
  • The result is the issued token.
  • Registering the same listener_id on the same topic more than once is allowed. Each registration is independent, has its own token, and receives its own delivery.

"publish" with arguments [topic, payload]

  • Look at all registrations currently active on topic.
  • The result is a list with one entry [listener_id, payload] per active registration, ordered by when the registrations were made (earliest first).
  • If the topic has no active registrations, or has never been used, the result is an empty list [].
  • payload can be any JSON value (including null, numbers, strings, lists, or objects) and must be returned unchanged.

"unsubscribe" with arguments [token]

  • If token belongs to a registration that is still active, remove it and return true.
  • Otherwise (the token was never issued, or was already cancelled) do nothing and return false.
  • Removing a registration must not change the relative order of the remaining ones, and tokens are never reused.

Output

Return a list of results, one per command, in the same order as the input. Results are integers (tokens), lists of [listener_id, payload] pairs, or booleans, as described above.

Constraints

  • operations and arguments have the same length.
  • Every command name is one of "subscribe", "publish", "unsubscribe".
  • Arguments always match the shape listed for their command.
  • Tokens are integers starting at 1; only subscribe commands consume tokens.
  • Delivery is purely a returned record; no real callbacks are executed.

Examples

Example 1

Input: replay_broker_log(['subscribe', 'subscribe', 'publish', 'unsubscribe', 'publish'], [['news', 'alice'], ['news', 'bob'], ['news', {'headline': 'Update'}], [1], ['news', 'next']])

Output: [1, 2, [['alice', {'headline': 'Update'}], ['bob', {'headline': 'Update'}]], True, [['bob', 'next']]]

Example 2

Input: replay_broker_log(['publish', 'subscribe', 'subscribe', 'unsubscribe', 'unsubscribe', 'unsubscribe', 'publish'], [['empty', 7], ['x', 'same'], ['x', 'same'], [1], [1], [2], ['x', None]])

Output: [[], 1, 2, True, False, True, []]

Hints

Hint 1

Keep, per topic, an ordered collection of active registrations so a publish can list them in the order they were made.

Hint 2

To cancel by token alone you need a second lookup from token to topic; removing the entry from that lookup also tells you whether the token was still active.

Hint 3

A publish only reports registrations that exist at that moment, so build its result right then and copy the payload as-is; repeated listener ids are separate registrations.