PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates a candidate's ability to refactor and implement clear, maintainable message-processing logic—covering code organization, command parsing, string trimming, state mutation, and edge-case handling in an in-memory chat store, and it tests competencies in coding, algorithms, and software engineering within the Coding & Algorithms category. It is commonly asked because it exposes a developer's capacity to produce deterministic, testable, and linear-time behavior while improving readability and maintainability in real-world backend message workflows, and its level of abstraction is primarily practical application with some conceptual design considerations.

  • hard
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Refactor a chat message processing function

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Task You are given a poorly written JavaScript function that processes an **incoming chat message** and updates an in-memory store. The code is hard to read, mixes concerns, and barely passes a small test. Your job is to **refactor and improve** it while preserving behavior and making it easier to maintain. ## Requirements Implement (or refactor toward) a clean version of: - `processIncomingMessage(store, input)` Where: - `store` is an in-memory object: - `store.messages`: array of stored messages - `store.userStatus`: map `{ [userId]: 'online' | 'away' | 'meeting' }` - `store.userTacoCount`: map `{ [userId]: number }` - `input` is: - `{ userId: string, text: string, ts: number }` The function must: 1. Trim the message text and ignore it if empty. 2. If the message starts with a **slash command**, handle these commands: - `/away` → set `store.userStatus[userId] = 'away'` and store a system message (role=`'system'`) like: `"<userId> is now away"`. - `/meet <topic>` → set status to `'meeting'` and store a system message: `"<userId> is in a meeting: <topic>"`. If `<topic>` is missing, use `"(no topic)"`. - `/taco` → increment `store.userTacoCount[userId]` and store a system message: `"<userId> sent a taco"`. 3. Otherwise, store it as a normal chat message with role=`'user'`. 4. Return the updated `store` (mutating is allowed, but your refactor should be clear and consistent). ## Expectations - Refactor for readability: extract helpers, avoid deeply nested conditionals, use clear naming. - Make behavior deterministic and easy to test. - Keep time complexity linear in `text.length`. ## Example Input: `{ userId: 'u1', text: ' /meet design review ', ts: 10 }` - Status becomes `meeting` - A system message is appended. Input: `{ userId: 'u1', text: 'hello', ts: 11 }` - A user message is appended. (You may assume a small provided unit test suite will validate exact stored messages and status updates.)

Quick Answer: This question evaluates a candidate's ability to refactor and implement clear, maintainable message-processing logic—covering code organization, command parsing, string trimming, state mutation, and edge-case handling in an in-memory chat store, and it tests competencies in coding, algorithms, and software engineering within the Coding & Algorithms category. It is commonly asked because it exposes a developer's capacity to produce deterministic, testable, and linear-time behavior while improving readability and maintainability in real-world backend message workflows, and its level of abstraction is primarily practical application with some conceptual design considerations.

Implement `solution(store, input)`, a clean and testable version of a chat message processor. `store` is a dictionary with: - `store['messages']`: a list of stored message objects - `store['userStatus']`: a map from `userId` to `'online'`, `'away'`, or `'meeting'` - `store['userTacoCount']`: a map from `userId` to an integer taco count `input` is a dictionary with: - `userId`: string - `text`: string - `ts`: integer timestamp Process the incoming message as follows: 1. Trim the message text. If it becomes empty, ignore it and return the store unchanged. 2. If the trimmed text is one of the supported slash commands, handle it: - `/away`: - set `store['userStatus'][userId] = 'away'` - append a system message with text `"<userId> is now away"` - `/meet <topic>`: - set `store['userStatus'][userId] = 'meeting'` - append a system message with text `"<userId> is in a meeting: <topic>"` - if the topic is missing or blank, use `"(no topic)"` - `/taco`: - increment `store['userTacoCount'][userId]` by 1, starting from 0 if needed - append a system message with text `"<userId> sent a taco"` 3. Otherwise, append a normal user message. Every appended message must be a dictionary with exactly these keys: - `role`: `'system'` or `'user'` - `userId`: the sender's user id - `text`: the stored message text - `ts`: the original timestamp Only `/away`, `/meet`, and `/taco` are special. Any other text, including unknown slash-prefixed text like `/shrug`, must be stored as a normal user message after trimming. You may mutate `store` in place, but return the updated `store`.

Constraints

  • 0 <= len(input['text']) <= 10^5
  • store always contains the keys 'messages', 'userStatus', and 'userTacoCount'
  • userId is a non-empty string
  • Only '/away', '/meet', and '/taco' are treated as commands
  • Your processing should run in O(n) time where n is the length of the message text

Examples

Input: ({'messages': [], 'userStatus': {}, 'userTacoCount': {}}, {'userId': 'u1', 'text': ' /meet design review ', 'ts': 10})

Expected Output: {'messages': [{'role': 'system', 'userId': 'u1', 'text': 'u1 is in a meeting: design review', 'ts': 10}], 'userStatus': {'u1': 'meeting'}, 'userTacoCount': {}}

Explanation: After trimming, this is a /meet command with topic 'design review'. Status becomes 'meeting' and a system message is appended.

Input: ({'messages': [], 'userStatus': {'u1': 'online'}, 'userTacoCount': {}}, {'userId': 'u1', 'text': ' hello team ', 'ts': 11})

Expected Output: {'messages': [{'role': 'user', 'userId': 'u1', 'text': 'hello team', 'ts': 11}], 'userStatus': {'u1': 'online'}, 'userTacoCount': {}}

Explanation: This is a normal chat message. The text is trimmed and stored with role 'user'.

Input: ({'messages': [{'role': 'user', 'userId': 'u2', 'text': 'hi', 'ts': 1}], 'userStatus': {'u1': 'online'}, 'userTacoCount': {'u1': 2}}, {'userId': 'u1', 'text': '/away', 'ts': 12})

Expected Output: {'messages': [{'role': 'user', 'userId': 'u2', 'text': 'hi', 'ts': 1}, {'role': 'system', 'userId': 'u1', 'text': 'u1 is now away', 'ts': 12}], 'userStatus': {'u1': 'away'}, 'userTacoCount': {'u1': 2}}

Explanation: The /away command updates the user's status and appends a system message, while preserving existing messages and taco counts.

Input: ({'messages': [], 'userStatus': {}, 'userTacoCount': {}}, {'userId': 'chef', 'text': '/taco', 'ts': 7})

Expected Output: {'messages': [{'role': 'system', 'userId': 'chef', 'text': 'chef sent a taco', 'ts': 7}], 'userStatus': {}, 'userTacoCount': {'chef': 1}}

Explanation: The /taco command increments the sender's taco count from 0 to 1 and stores a system message.

Input: ({'messages': [], 'userStatus': {}, 'userTacoCount': {}}, {'userId': 'u3', 'text': ' /meet ', 'ts': 20})

Expected Output: {'messages': [{'role': 'system', 'userId': 'u3', 'text': 'u3 is in a meeting: (no topic)', 'ts': 20}], 'userStatus': {'u3': 'meeting'}, 'userTacoCount': {}}

Explanation: The /meet command has no topic after trimming, so '(no topic)' is used.

Input: ({'messages': [], 'userStatus': {}, 'userTacoCount': {}}, {'userId': 'u9', 'text': ' /shrug ', 'ts': 33})

Expected Output: {'messages': [{'role': 'user', 'userId': 'u9', 'text': '/shrug', 'ts': 33}], 'userStatus': {}, 'userTacoCount': {}}

Explanation: Only /away, /meet, and /taco are special. Unknown slash-prefixed text is stored as a normal user message.

Input: ({'messages': [{'role': 'user', 'userId': 'u1', 'text': 'x', 'ts': 1}], 'userStatus': {'u1': 'online'}, 'userTacoCount': {'u1': 1}}, {'userId': 'u1', 'text': ' ', 'ts': 30})

Expected Output: {'messages': [{'role': 'user', 'userId': 'u1', 'text': 'x', 'ts': 1}], 'userStatus': {'u1': 'online'}, 'userTacoCount': {'u1': 1}}

Explanation: After trimming, the message is empty, so the store is returned unchanged.

Hints

  1. Trim the text once at the start and return early if nothing remains.
  2. A small helper for appending message objects can remove repeated code and make command handling clearer.
Last updated: Apr 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Simulate Infection Spread on a Grid - OpenAI (hard)
  • Implement Social Follow Recommendations - OpenAI (medium)
  • Build a Compose Rating Card - OpenAI (medium)
  • Generate Data Labeling Schedules - OpenAI (medium)
  • Convert IPv4 Ranges to CIDR Blocks - OpenAI (medium)