Design a Molecular Reactor Queue with a Bounded Cooling Chamber and Wait Estimates

Quick Overview

Design a reactor that processes samples one at a time with a bounded waiting queue, supporting adding samples, starting the next one, estimating wait times and reporting the queue state. It tests choosing data structures for arrival order and fast duplicate checks, and handling idle, full and duplicate cases cleanly.

Design a Molecular Reactor Queue with a Bounded Cooling Chamber and Wait Estimates

Company: Plaid

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a molecular reactor queue system that simulates a reactor processing chemical samples one at a time. Each sample takes exactly 300 seconds to process. The reactor has a bounded cooling chamber queue of a given `capacity` that holds samples waiting to be processed. When a new sample arrives, if the reactor is idle (nothing processing and queue empty), it begins processing immediately. Otherwise, the sample is placed in the cooling chamber queue. If the queue is already full, the sample is rejected. Implement a `MolecularReactor` class, constructed with `capacity`, with these methods: - `addSample(sample_id: str) -> str` adds a sample to the reactor. It returns `"processing"` if the reactor is idle and the sample starts immediately, `"accepted"` if the sample is added to the cooling chamber queue, or `"rejected"` if the queue is full or the sample ID is a duplicate. - `startProcessing() -> str` finishes the current sample and begins processing the next sample in the queue. It returns the ID of the sample that starts processing, or `"idle"` if the queue is empty. - `getWaitTime(sample_id: str) -> int` returns the estimated wait time in seconds for the given sample: 0 if the sample is currently being processed, the computed wait time if it is in the queue, or -1 if the sample is not found. - `getQueueState() -> List[str]` returns the current state as a list. The first element is the ID of the sample being processed (or `"idle"` if none), followed by the samples in the cooling chamber queue in order. Example 1: ```text Input: ["MolecularReactor", "addSample", "addSample", "addSample", "addSample", "addSample", "getWaitTime", "getQueueState", "startProcessing", "getWaitTime", "getWaitTime"] [[2], ["S1"], ["S2"], ["S2"], ["S3"], ["S4"], ["S3"], [], [], ["S3"], ["S1"]] Output: [null, "processing", "accepted", "rejected", "accepted", "rejected", 600, ["S1", "S2", "S3"], "S2", 300, -1] reactor = MolecularReactor(2) # cooling chamber capacity = 2 reactor.addSample("S1") # "processing": reactor idle, S1 starts immediately reactor.addSample("S2") # "accepted": S2 waits in the chamber reactor.addSample("S2") # "rejected": S2 is already waiting (duplicate) reactor.addSample("S3") # "accepted": the chamber now holds S2, S3 reactor.addSample("S4") # "rejected": the chamber is full reactor.getWaitTime("S3") # 600: S1 is in the reactor, then S2 is ahead reactor.getQueueState() # ["S1", "S2", "S3"] reactor.startProcessing() # "S2": S1 finishes, S2 moves into the reactor reactor.getWaitTime("S3") # 300: only S2 is ahead now reactor.getWaitTime("S1") # -1: S1 has finished and is not found ``` Example 2: ```text Input: ["MolecularReactor", "addSample", "startProcessing", "getQueueState", "addSample", "getWaitTime"] [[1], ["A1"], [], [], ["A2"], ["A2"]] Output: [null, "processing", "idle", ["idle"], "processing", 0] reactor = MolecularReactor(1) # cooling chamber capacity = 1 reactor.addSample("A1") # "processing": reactor idle, A1 starts immediately reactor.startProcessing() # "idle": queue empty, reactor becomes idle reactor.getQueueState() # ["idle"] reactor.addSample("A2") # "processing": reactor idle again, A2 starts immediately reactor.getWaitTime("A2") # 0: A2 is currently processing ``` ```hint Match structures to operations List which methods need the samples in arrival order and which only need a fast "is this ID already here?" check. One structure rarely gives you both cheaply. ``` ```hint Counting the wait For a waiting sample, count every sample that has to leave the reactor before it can go in, and remember the one being processed right now. ``` ### Constraints & Assumptions - `1 <= capacity <= 1000`. The capacity counts waiting samples only; the sample in the reactor does not take a chamber slot. - Sample IDs are non-empty strings. - At most 10,000 calls are made across all methods. - Each sample takes exactly 300 seconds to process. - The class keeps no clock, so a wait estimate counts the sample in the reactor as a full 300 seconds (as in Example 1). ### Clarifying Questions to Ask - Does "duplicate" mean an ID that is currently processing or waiting, or any ID the reactor has ever seen? - What should `startProcessing` do when the reactor is already idle and the queue is empty? - Can a sample ID be the literal string `"idle"`, which would collide with the idle marker in the return values? - Will the class be called from more than one thread? ### What a Strong Answer Covers - The data structures chosen for arrival order and for constant-time duplicate checks, and how they are kept in sync. - How `addSample` tells apart a duplicate, an idle reactor, a full chamber and a normal enqueue. - The state change in `startProcessing`, including the empty-queue case that returns the reactor to idle. - The wait-time formula for a queued sample, plus the 0 and -1 cases. - Time and space complexity per method, and the choice of marker for the idle state. ### Follow-up Questions - How would you make `getWaitTime` run in constant time? - If the reactor knew when the current sample started, how would the wait estimates change? - How would you make the class safe to call from several threads at once? - What changes if each sample can have its own processing time?

Overview: Design a reactor that processes samples one at a time with a bounded waiting queue, supporting adding samples, starting the next one, estimating wait times and reporting the queue state. It tests choosing data structures for arrival order and fast duplicate checks, and handling idle, full and duplicate cases cleanly.

Plaid logo
Plaid
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
4
0

Design a molecular reactor queue system that simulates a reactor processing chemical samples one at a time. Each sample takes exactly 300 seconds to process.

The reactor has a bounded cooling chamber queue of a given capacity that holds samples waiting to be processed. When a new sample arrives, if the reactor is idle (nothing processing and queue empty), it begins processing immediately. Otherwise, the sample is placed in the cooling chamber queue. If the queue is already full, the sample is rejected.

Implement a MolecularReactor class, constructed with capacity, with these methods:

  • addSample(sample_id: str) -> str adds a sample to the reactor. It returns "processing" if the reactor is idle and the sample starts immediately, "accepted" if the sample is added to the cooling chamber queue, or "rejected" if the queue is full or the sample ID is a duplicate.
  • startProcessing() -> str finishes the current sample and begins processing the next sample in the queue. It returns the ID of the sample that starts processing, or "idle" if the queue is empty.
  • getWaitTime(sample_id: str) -> int returns the estimated wait time in seconds for the given sample: 0 if the sample is currently being processed, the computed wait time if it is in the queue, or -1 if the sample is not found.
  • getQueueState() -> List[str] returns the current state as a list. The first element is the ID of the sample being processed (or "idle" if none), followed by the samples in the cooling chamber queue in order.

Example 1:

Input:
["MolecularReactor", "addSample", "addSample", "addSample", "addSample", "addSample", "getWaitTime", "getQueueState", "startProcessing", "getWaitTime", "getWaitTime"]
[[2], ["S1"], ["S2"], ["S2"], ["S3"], ["S4"], ["S3"], [], [], ["S3"], ["S1"]]

Output: [null, "processing", "accepted", "rejected", "accepted", "rejected", 600, ["S1", "S2", "S3"], "S2", 300, -1]

reactor = MolecularReactor(2)   # cooling chamber capacity = 2
reactor.addSample("S1")         # "processing": reactor idle, S1 starts immediately
reactor.addSample("S2")         # "accepted": S2 waits in the chamber
reactor.addSample("S2")         # "rejected": S2 is already waiting (duplicate)
reactor.addSample("S3")         # "accepted": the chamber now holds S2, S3
reactor.addSample("S4")         # "rejected": the chamber is full
reactor.getWaitTime("S3")       # 600: S1 is in the reactor, then S2 is ahead
reactor.getQueueState()         # ["S1", "S2", "S3"]
reactor.startProcessing()       # "S2": S1 finishes, S2 moves into the reactor
reactor.getWaitTime("S3")       # 300: only S2 is ahead now
reactor.getWaitTime("S1")       # -1: S1 has finished and is not found

Example 2:

Input:
["MolecularReactor", "addSample", "startProcessing", "getQueueState", "addSample", "getWaitTime"]
[[1], ["A1"], [], [], ["A2"], ["A2"]]

Output: [null, "processing", "idle", ["idle"], "processing", 0]

reactor = MolecularReactor(1)   # cooling chamber capacity = 1
reactor.addSample("A1")         # "processing": reactor idle, A1 starts immediately
reactor.startProcessing()       # "idle": queue empty, reactor becomes idle
reactor.getQueueState()         # ["idle"]
reactor.addSample("A2")         # "processing": reactor idle again, A2 starts immediately
reactor.getWaitTime("A2")       # 0: A2 is currently processing

Constraints & Assumptions

  • 1 <= capacity <= 1000 . The capacity counts waiting samples only; the sample in the reactor does not take a chamber slot.
  • Sample IDs are non-empty strings.
  • At most 10,000 calls are made across all methods.
  • Each sample takes exactly 300 seconds to process.
  • The class keeps no clock, so a wait estimate counts the sample in the reactor as a full 300 seconds (as in Example 1).

Clarifying Questions to Ask Guidance

  • Does "duplicate" mean an ID that is currently processing or waiting, or any ID the reactor has ever seen?
  • What should startProcessing do when the reactor is already idle and the queue is empty?
  • Can a sample ID be the literal string "idle" , which would collide with the idle marker in the return values?
  • Will the class be called from more than one thread?

What a Strong Answer Covers Guidance

  • The data structures chosen for arrival order and for constant-time duplicate checks, and how they are kept in sync.
  • How addSample tells apart a duplicate, an idle reactor, a full chamber and a normal enqueue.
  • The state change in startProcessing , including the empty-queue case that returns the reactor to idle.
  • The wait-time formula for a queued sample, plus the 0 and -1 cases.
  • Time and space complexity per method, and the choice of marker for the idle state.

Follow-up Questions Guidance

  • How would you make getWaitTime run in constant time?
  • If the reactor knew when the current sample started, how would the wait estimates change?
  • How would you make the class safe to call from several threads at once?
  • What changes if each sample can have its own processing time?