solidcodersolidcoder
Explore Courses
solidcodersolidcoder

Practical courses for software engineering interviews — no gatekeeping, no fluff.

Company-wise Questions

  • Flipkart Machine Coding Questions

Machine Coding Tutorial

  • Machine Coding Tutorial

Explore

  • Courses
  • About
  • Privacy Policy
  • Terms

© 2026 solidcoder · Practical courses for software engineering interviews.

Built for the AI era — learn by doing.

Home/Machine Coding/Machine coding Tutorial/Design an In-Memory Message Queue
Chapters — Machine coding Tutorial▾

Design an In-Memory Message Queue

Machine Coding·Machine Coding Questions·5 min read·Sep 11, 2026

Build a thread-safe, in-memory message queue that supports multiple topics, publishers, subscribers, and concurrent message delivery using the publish-subscribe model.

Asked inDream11UberSuperMoney

Design and implement an in-memory message queue that follows the Publisher-Subscriber (Pub/Sub) model.

The system should allow publishers to send messages to topics, while multiple subscribers can listen to one or more topics. Whenever a message is published, every subscriber registered for that topic should receive it.

The system must also support concurrent publishing and consumption, allowing multiple producers and consumers to operate independently and in parallel.

Core Idea

The system can be thought of as:

                 ┌──────────────┐
                 │   Producer   │
                 └──────┬───────┘
                        │ publish
                        ▼
                ┌─────────────────┐
                │      Topic      │
                └────────┬────────┘
                         │
              ┌──────────┼──────────┐
              ▼          ▼          ▼
         Subscriber A Subscriber B Subscriber C

A subscriber registered with a topic should receive every message published to that topic.

What the round tests

This one tests whether you can model pub/sub fan-out cleanly — topic isolation, subscriber registry, and ordered broadcast — while keeping delivery thread-safe and non-blocking when multiple producers and consumers run in parallel.

Objective

The primary objective is to design and implement a thread-safe, in-memory message queue that supports multiple independent topics, multiple publishers per topic, subscribers listening to many topics, and concurrent broadcast delivery — with extensibility for unsubscribe, ordering, and failure isolation.

Functional Requirements

Three tiers of scope

Requirements are split into three tiers so you know what to prioritize under time pressure: build first, build if time allows, and guard concurrency correctly.

Part 1 — Basic Requirements

Core functionality you must build and get working first.

1. Topics

Topic rules
Rule
Detail
Multiple topics
The queue supports many independent topics acting as channels.
Isolation
Messages published to one topic are never delivered to subscribers of another.
Examples
orders, payments, notifications — each topic is independent.

2. Publishers

Publisher rules
Rule
Detail
Multiple publishers
Many producers can publish concurrently to any topic.
String payload
A publisher sends a string message to a topic (e.g., “Order Created”).
Multi-topic
A single publisher can publish to many topics — P1 → orders, P2 → payments.

3. Subscribers

Subscriber rules
Rule
Detail
Multiple subscribers
Many consumers can subscribe; a subscriber can listen to 1..N topics.
Topic filter
A subscriber receives only from topics it is subscribed to.
Example
C1 → orders + payments, C2 → orders — each gets only its topics.

4. Message Delivery

Delivery rule
Rule
Detail
Fan-out
Every message published to a topic is delivered to all subscribers currently subscribed to that topic.

For example:

Topic: orders → Subscribers: C1, C2, C3 → Message: "Order #101 created"
→ C1 received Order #101 created
  C2 received Order #101 created
  C3 received Order #101 created

5. Multiple Topic Subscriptions

A subscriber should be allowed to listen to multiple topics and receive messages from all of them.

C1 → orders, payments, notifications — receives from all three.

6. In-Memory Storage

No persistence

The entire queue operates in memory. No database, files, or external storage — focus on topic management, subscription handling, and concurrent delivery.

Part 2 — Bonus Features

Bonus — score higher if time allows

Extensibility checks — can your design add unsubscribe and delivery guarantees without touching the core broadcast path.

Bonus features
Feature
What to build
Unsubscribe
Allow a subscriber to stop listening to a topic and no longer receive new messages.
Subscriber-specific processing
Each subscriber processes independently (e.g., analytics vs notification vs inventory) without blocking others.
Message ordering
Messages on the same topic are delivered in publish order — A, B, C stays A, B, C.
Consumer failure handling
Failure of one consumer does not prevent delivery to others.

Part 3 — Concurrency & Delivery Guarantees

1. Multiple Producers and Consumers

Producer 1 ──┐
             ├──► Topic A
Producer 2 ──┘
Consumer 1 ──┐
Consumer 2 ──┼──► Topic A
Consumer 3 ──┘

2. Concurrent Publishing

Concurrent publishing
Rule
Detail
Parallel publishes
P1→Topic A “A”, P2→Topic A “B”, P3→Topic B “C” can happen simultaneously.
Thread safety
Topic registry and message queues must be safely accessed under concurrency.

3. Concurrent Consumption

Subscribers should process messages independently and in parallel — a slow consumer must not block others.

4. Thread Safety

Shared state to guard
Resource
Risk
Topic registry
Concurrent create / lookup of topics.
Subscriber lists
Add / remove subscriber while publishing.
Message queues
Enqueue by publishers, dequeue by consumers.
Subscription map
Reads during broadcast vs writes on subscribe/unsubscribe.
Race example

Thread 1 → adding a subscriber, Thread 2 → publishing, Thread 3 → removing a subscriber — all at once. Protect with concurrent collections, locks, or copy-on-write snapshots.

5. Delivery Isolation

Isolation rule
Rule
Detail
Non-blocking fan-out
Deliver to each subscriber on its own execution path — C1/C3 should not wait for slow C2 (5s) on Topic payments.

6. Message Output

When a subscriber receives a message, print:

<consumer_id> received <message>

For example:

consumer1 received Payment Successful
consumer2 received Payment Successful

Example Usage: Full Walkthrough

Here's how a sample session runs end-to-end.

1. Create topics

topicA
topicB

2. Create producers

producerA
producerB

3. Create consumers

consumerA, consumerB, consumerC, consumerD, consumerE

4. Subscriptions

topicA: consumerA, consumerB, consumerC, consumerD, consumerE
topicB: consumerA, consumerC, consumerE

5. Publishing messages

producerA → topicA → "Order Created"
producerA → topicA → "Order Confirmed"
producerB → topicA → "Order Shipped"

producerA → topicB → "Payment Initiated"
producerB → topicB → "Payment Completed"

6. Expected behavior

Three messages on topicA are received by all five subscribers:

consumerA received Order Created
consumerB received Order Created
consumerC received Order Created
consumerD received Order Created
consumerE received Order Created

Two messages on topicB are received only by:

consumerA, consumerC, consumerE
→ consumerA received Payment Initiated
  consumerC received Payment Initiated
  consumerE received Payment Initiated
Ordering note

Exact output ordering may vary because consumers are allowed to execute concurrently — but per-topic order per subscriber must be preserved.

Result

The queue correctly fans out per-topic — five subscribers get all three topicA messages, only the three subscribed to topicB get its two messages, and concurrent publishers/consumers make progress without racing on the registry.

What They Looked For

Evaluation criteria
Criterion
What's assessed
Demoable & correct
Pub/Sub works end-to-end with multiple topics and subscribers.
Pub/Sub design
Topic, publisher, subscriber, and broker boundaries are clean.
Thread safety
Registry and subscriber lists are guarded correctly.
Concurrency
Concurrent publish and independent consumption work safely.
Clean abstractions
Producer-consumer coordination is decoupled and testable.
Extensibility
Unsubscribe, ordering, and failure handling slot in without rewrite.
Up next7/8
Part 2 · Machine Coding Questions
←
← Prev Chapter
Parking Lot
5 min
Next Chapter →
Key-Value Store
5 min · continue reading
→
This month's launch price
₹30,000₹3,000SOLID50 — 50% OFF

3 years access · 40+ lessons · AI assisted coding

Enroll Now →

Use code SOLID50 at checkout

On this page
  • Objective
  • Functional Requirements
    • Part 1 — Basic Requirements
    • Part 2 — Bonus Features
    • Part 3 — Concurrency & Delivery Guarantees
  • Example Usage: Full Walkthrough
  • What They Looked For