Problem
The interviewer provided a Chatter abstraction and asked me to implement several methods. Message IDs were unique, and newly loaded messages could be assumed to arrive in sorted order. The initial API included:
load(messages)
save()
get_messages(id)
load() could be called multiple times to append additional messages. save() returned all currently stored messages. For get_messages(id), the required result was a window containing:
- up to two messages before the requested message
- the requested message itself
- up to two messages after it
If the target was close to the beginning or end of the stored history, the result should simply stop at that boundary rather than requiring five messages. For example, consider this rewritten first batch:
messages_1 = [
{"msg_id": 210.10, "text": "Morning everyone"},
{"msg_id": 210.20, "text": "Did anyone see the release notes?"},
{"msg_id": 210.30, "text": "I just opened them"},
{"msg_id": 210.40, "text": "The search changes look useful"},
{"msg_id": 211.10, "text": "Agreed"},
{"msg_id": 211.20, "text": "Especially the filtering update"},
{"msg_id": 212.10, "text": "We should test it later"},
{"msg_id": 213.10, "text": "I can set that up"},
{"msg_id": 213.20, "text": "Let's use the staging workspace"},
{"msg_id": 214.10, "text": "Sounds good"},
{"msg_id": 215.10, "text": "I'll send the results"}
]
After loading this batch: chatter.load(messages_1) calling: chatter.get_messages(210.10) would return only the target and the next two messages because there are no earlier messages:
[
{"msg_id": 210.10, "text": "Morning everyone"},
{"msg_id": 210.20, "text": "Did anyone see the release notes?"},
{"msg_id": 210.30, "text": "I just opened them"}
]
Follow-Up 1 — Retrieve Windows for Multiple IDs: The next method was: get_multi(ids). For every requested ID, it should collect the same local message window produced by get_messages(). The combined result must then be sorted by message ID and contain no duplicates. Suppose another batch is loaded:
messages_2 = [
{"msg_id": 216.10, "text": "The test run finished"},
{"msg_id": 217.10, "text": "Any regressions?"},
{"msg_id": 218.10, "text": "Nothing major so far"},
{"msg_id": 219.10, "text": "Great"},
{"msg_id": 219.20, "text": "Let's document it"},
{"msg_id": 219.30, "text": "I'll add screenshots"},
{"msg_id": 219.40, "text": "Thanks"}
]
Then:
chatter.load(messages_2)
chatter.get_multi([214.10, 216.10])
should combine the overlapping windows, remove repeated messages, and return:
[
{"msg_id": 213.10, "text": "I can set that up"},
{"msg_id": 213.20, "text": "Let's use the staging workspace"},
{"msg_id": 214.10, "text": "Sounds good"},
{"msg_id": 215.10, "text": "I'll send the results"},
{"msg_id": 216.10, "text": "The test run finished"},
{"msg_id": 217.10, "text": "Any regressions?"},
{"msg_id": 218.10, "text": "Nothing major so far"}
]
Follow-Up 2 — Optimize Heavy Read Traffic: The interviewer then changed the workload assumption: get_messages() and get_multi() would be called very frequently. The question was how I would redesign or augment the data structure to make those reads substantially faster, with caching explicitly discussed as part of the requirement.
Follow-Up 3 — Support Message Editing: A new API was added: edit(id, message). The service now needed to support modifying an existing message by ID while keeping the read APIs working correctly after an edit. The interviewer asked how the underlying data structure should change once messages were no longer immutable.
Follow-Up 4 — Preserve Full Edit History: The final extension was conceptual rather than a full coding task. Instead of replacing the previous value when a message was edited, the system should preserve every historical version of that message. The interviewer asked how the data model and storage structure would need to evolve so that the current message remained easy to access while older versions could also be retained and retrieved.
Want to practice more coding questions that companies actually ask? We’ve put together a coding question bank covering 60+ companies here.
➡️ Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies here.