FastPrepMachine Message Router

Machine Message Router

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process an ordered sequence of operations for an in-memory machine message router. Each operation is one of:

  • REGISTER|machineId: register the machine. Registering an existing machine is a no-op and produces no output.
  • SEND|machineId|message: if the machine is registered, append the complete message to its unread inbox and output true. Otherwise, output false and discard the message.
  • READ|machineId: output all unread messages for that machine in send order, joined by semicolons, then clear its inbox. For an unregistered machine or an empty inbox, output an empty string.

Machine IDs are case-sensitive. Messages are customizable strings: they may contain spaces and punctuation other than pipes or semicolons. Return outputs only for SEND and READ operations, in operation order.

Function

processMachineMessages(operations: String[]) → String[]

Examples

Example 1

operations = ["REGISTER|alpha","SEND|alpha|hello world","SEND|beta|lost","READ|alpha","READ|alpha"]return = ["true","false","hello world",""]

The first message is delivered to the registered machine. The send to beta fails. Reading alpha returns and clears its inbox, so the next read is empty.

Example 2

operations = ["REGISTER|m1","REGISTER|m1","SEND|m1|build #42 ready!","SEND|m1|deploy at 09:30","READ|m1","SEND|m1|done","READ|m1"]return = ["true","true","build #42 ready!;deploy at 09:30","true","done"]

Repeated registration preserves the inbox. Custom punctuation and spaces remain unchanged, and each read returns only messages sent since the previous read.

Constraints

  • 0 <= operations.length <= 200000.
  • Every operation has exactly the syntax shown above.
  • 1 <= machineId.length <= 40, using English letters, digits, underscores, or hyphens.
  • 1 <= message.length <= 200, using printable ASCII other than | and ;.
  • The total length of all operation strings is at most 2000000.

More OpenAI problems

See OpenAI hiring insights
public String[] processMachineMessages(String[] operations) {
    // write your code here
}
operations["REGISTER|alpha","SEND|alpha|hello world","SEND|beta|lost","READ|alpha","READ|alpha"]
expected["true", "false", "hello world", ""]
Checking account…