Design a Snapchat-Style Ephemeral Messenger with TTL Texts and View-Once Snaps
Company: Snapchat
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a messaging system where messages have a time-to-live (TTL), supporting different message types and deletion triggers. The system models ephemeral messaging similar to Snapchat.
The system supports two message types: `text` messages that expire after a specified TTL (in seconds) from the time they are sent, and `snap` messages that expire immediately after being viewed once, regardless of any TTL value. A text sent at timestamp `t` with TTL `d` is active while the current timestamp is less than `t + d`.
Messages are assigned auto-incrementing IDs starting from `1`. All timestamp parameters are integers representing seconds. Timestamps across calls are guaranteed to be non-decreasing.
Implement a class `EphemeralMessenger` with the following methods:
- `sendMessage(sender: str, recipient: str, content: str, messageType: str, timestamp: int, ttl: int) -> int`: Send a message from sender to recipient with the given content, type (`'text'` or `'snap'`), timestamp, and TTL (in seconds). For snap messages, TTL is ignored since they expire after one view. Returns the unique message ID.
- `viewMessage(messageId: int, timestamp: int) -> str`: View a message by its ID at the given timestamp. Returns the message content if the message is still active. Returns an empty string if the message is expired, already viewed (for snaps), or does not exist. Viewing a snap marks it as expired after this view.
- `getInbox(userId: str, timestamp: int) -> List[List]`: Retrieve all active (non-expired) messages for the given user at the given timestamp. Returns a list of `[messageId, sender, content, messageType]` sorted by message ID in ascending order.
- `deleteExpiredMessages(timestamp: int) -> int`: Delete all expired messages from the system at the given timestamp. Returns the number of messages deleted.
For example:
```text
m = EphemeralMessenger()
m.sendMessage("alice", "bob", "lunch at noon?", "text", 10, 30) -> 1
m.sendMessage("carol", "bob", "look at this", "snap", 12, 0) -> 2
m.sendMessage("alice", "bob", "running late", "text", 15, 5) -> 3
m.getInbox("bob", 16)
-> [[1, "alice", "lunch at noon?", "text"], [2, "carol", "look at this", "snap"], [3, "alice", "running late", "text"]]
m.viewMessage(2, 18) -> "look at this"
m.viewMessage(2, 19) -> ""
m.getInbox("bob", 20) -> [[1, "alice", "lunch at noon?", "text"]]
m.deleteExpiredMessages(25) -> 2
m.viewMessage(1, 39) -> "lunch at noon?"
m.viewMessage(1, 40) -> ""
m.deleteExpiredMessages(40) -> 1
```
Message 3 was sent at 15 with a TTL of 5, so it is expired from timestamp 20 on, and snap 2 is gone after its single view at 18. The cleanup at 25 removes those two. Message 1 stays readable through 39 and expires at 40, and the last cleanup removes it.
```hint Two kinds of expiry
Write down, for each message type, the exact condition that makes it inactive at a given timestamp, including the moment the TTL runs out. Every method can then reuse that one check.
```
```hint Cleanup cost
A sweep over every stored message works. Think about what you could record at send time and at view time so that a cleanup call touches only the messages it deletes.
```
### Constraints & Assumptions
- `1 <= userId.length <= 20`
- `1 <= content.length <= 500`
- `messageType` is either `'text'` or `'snap'`
- `0 <= timestamp <= 10^9`
- `1 <= ttl <= 86400` (for text messages)
- At most `10^4` calls will be made to any method.
- Timestamps are non-decreasing across calls.
### Clarifying Questions to Ask
- Can anyone view a message by its ID, or only its recipient?
- Does an unopened snap ever expire by time, and does viewing a text change its state at all?
- How should invalid input be handled, such as an unknown message type or a text sent without a positive TTL?
- How often will `deleteExpiredMessages` run, and do inbox reads need to stay fast if it rarely runs?
### What a Strong Answer Covers
- A message model and indexes that keep send, view and inbox reads cheap, with the inbox order guaranteed ascending by ID.
- The exact expiry rule for each type, including the boundary at send time plus TTL and the single view allowed for a snap.
- How `deleteExpiredMessages` finds expired messages, what it costs, and how it keeps the per-user index consistent.
- Why non-decreasing timestamps let expiry be treated as permanent.
- Edge cases such as unknown IDs and users, repeated views, views after deletion and invalid message types.
### Follow-up Questions
- How would you restrict `viewMessage` so that only the recipient can open a message?
- How does the model change for group chats, where a snap should disappear only after every member has viewed it?
- Two devices of the same user open the same snap at the same moment. How do you make sure it is shown once?
- How would you move this onto a real storage layer where deletion must actually remove the content?
Overview: Design an ephemeral messaging class where text messages expire after a TTL and snaps disappear after a single view. It tests exact expiry rules at the boundary, inbox ordering by message ID, the cost of cleanup, and keeping indexes consistent as messages are deleted.