Build it. The database schema, API, code and platform limits for a chat app are on a separate page: Build it: a chat app on Android and iOS (14 min read).
Try it. Send a message on a bad connection in the offline chat simulator, change the retry rules in the retry explorer, or expire a token under parallel requests in the token refresh explorer.
How to read this. This page is the 45 minutes, in the order I would spend them. Schema, API, code and platform tables are in the build guide. The follow-up questions are my reconstruction from public prep material, not transcripts of real rounds.
TL;DR. Room (Android's on-device database) is the source of truth, the one copy the app trusts. The screen only reads it. A send is a
PENDINGrow that WorkManager (Android's scheduler for work that survives restarts) delivers. Messages come by WebSocket (a two-way open connection) on screen, and as an FCM (Google's push service) hint when it is closed. A message may arrive twice. A client-generated ID makes that harmless. On iOS: the same design, with SwiftData or Core Data as the database and my own send loop, because iOS has no WorkManager. The push is an alert push plus a notification extension, because silent pushes are unreliable.
Patterns in this round.
- Outbox (a send is a pending row that a worker delivers)
- Repository (the on-device database is the one copy the app trusts)
- Observer (the screen watches the database)
- Idempotency key (a client-made ID makes a repeat harmless)
More in design patterns in mobile rounds.
Free: one follow-up round a week. A short interview question with a weak and a strong answer, by email. You also get the cheat sheet.
1. Scope and constraints (minutes 0 to 4)
In short: Keep groups, delay images and a second phone, and say what the phone does to the design.
A prompt I would expect: "Design the chat feature for our app. People message one to one and in small groups." I ask five questions and give a default for each:
- How much is the phone? The app and its API contract.
- Groups? Yes, up to about 200 members.
- Offline? Yes, read and write with no signal.
- Receipts? Sent, delivered, read.
- Media and devices? Text only, one phone each.
| In scope | Out of scope | Left out until you ask |
|---|---|---|
| One to one and group chat | Voice and video calls | Images and video |
| Text, history, writing offline | Search | A second phone |
| Sent, delivered, read | End-to-end encryption | |
| A notification when the app is closed |
The phone does four things to this design:
| What the phone does | Requirement at risk |
|---|---|
| Stops the app process (the running copy of the app) without warning | A tap on send must not be lost |
| Stops network access and delays background work | Messages arrive while the app is closed |
| Loses the radio signal during a request | No lost messages, no doubles, right order |
| Runs on a battery and a data plan | No polling loop, no permanent socket |
Your decision. The interviewer says: "Groups, images and two devices too." Which do you keep?
First idea, and why it fails. Say yes to all three. Time runs out before I draw the offline send path.
What I would say. Groups stay, because they mostly change receipts. Images and a second phone each change the whole send path, so they wait.
Say this. "Groups are in. Images, a second phone and encryption each change the send path, so they wait. Tell me if you want one earlier."
The follow-up question I expect: "Everyone sends photos."
They return as follow-up 2.
45-minute plan
| Minutes | Step | Drawn by the end | Cut if behind |
|---|---|---|---|
| 0 to 4 | Scope and constraints | Scope list, phone behaviors | Fewer questions |
| 4 to 14 | Data and contracts | Room as truth, clientId, seq, four calls | Receipts, numbers |
| 14 to 22 | Client structure | Layers, who owns the socket and sends | One line per layer |
| 22 to 40 | Make the hard part work | Send path drawn, receive as a hint plus a sync (ask the server for new data) | Receive in three sentences |
| 40 to 45 | Break it, ship it, roll it back | Top failures, the flag, what I would watch | Two failures and the flag |
2. Data and contracts (minutes 4 to 14)
In short: The local database is the source of truth. Each message gets an ID made on the device. The server sets the order.
Your decision. Where does a message live? In memory behind the ViewModel (the class that holds a screen's state), on the server with a disk cache, or in a local database that the screen watches?
First idea, and why it fails. Hold messages in memory and fetch them when the screen opens. When Android stops the process, every unsent message is lost. A disk cache beside a server list is better, but the two can disagree.
What I would say. The screen reads the local database and nothing else. The network writes into it. This gives offline reading and instant sends.
The follow-up question I expect: "The same message can arrive by socket, by a push-triggered sync, and in the response to your own send. How many rows?"
One. Each message gets a clientId, a UUID (a random unique ID) made on the device. A unique index (a database rule that refuses a repeated value) blocks a second row. The code then does an upsert: insert the row, or update it if it exists.
- The order comes from
seq, a sequence number that the server assigns in each conversation. Device clocks never set the order. - A pending message stays at the bottom until its ack (the server's confirmation) brings a
seq.
The contract:
- Send:
clientIdand body in,serverIdandseqout. The sameclientIdalways gets the same answer. - Catch up: everything in a conversation after a given
seq. - Cold start (the app starts fresh): which conversations changed since a cursor (a saved position).
- Socket: the server pushes messages and status changes. The client sends
ack(seq).
These are my own assumptions, not measurements:
| Assumption | Arithmetic | What it decides |
|---|---|---|
| A heavy user: 200 text messages a day, about 300 bytes each | 60 KB a day, about 22 MB a year | No rule to delete old text. I set a limit only for media |
| 10 minutes offline, a message every 10 s, about 300 ms per ack | 60 x 0.3 s is 18 s to send them all | One at a time, in order, no batch endpoint |
| Retry delays double from 10 s, eight sends, FAILED on the ninth run | 10 + 20 + ... + 1,280 s is 2,550 s, about 42 minutes | An outage under about 21 minutes (the eighth send) ends with delivered messages |
3. Client structure (minutes 14 to 22)
In short: Sending and the socket sit below the screen: an outbox for sends, and one engine for the socket.
Your decision. Who owns sending and the socket? The chat screen's ViewModel, a service, or a component that lives as long as the app?
First idea, and why it fails. The ViewModel. It holds the screen state, so it opens the socket and starts each send in viewModelScope. But Android clears a ViewModel when its screen leaves the back stack, the list of opened screens (ViewModel). The socket and any send in progress are lost. SavedStateHandle survives process death, but holds only small UI state (saved state).
What I would say. Ownership goes below the screen:
- The socket belongs to a sync engine, a class that lives as long as the app. It keeps the socket open while the app is on screen.
- Sends belong to the outbox, a table of waiting messages. It holds
PENDINGrows in Room, and a WorkManager worker (a class that does one task) sends them. WorkManager documents that its work persists across app restarts and device reboots (WorkManager). - The ViewModel only asks the repository (the class that reads and writes data) to insert a row.
I reject a service. On Android 12 and later, an app in the background cannot start a foreground service, except in a few special cases (restrictions).
The follow-up question I expect: "Two screens both want the socket. Who closes it?"
The sync engine closes it when the app leaves the screen. Screens share one socket.
More links
- Repository to Outbox worker: enqueue
- Outbox worker to REST API: send
- WebSocket + push to Sync engine: events
- Sync engine to Local database: writes
The screen and ViewModel are the UI layer, and ChatRepository is the data layer. I skip a domain layer until two ViewModels share logic (recommendations). Three rules:
- The UI never talks to the network. It observes Room.
- Socket and push handlers write into Room through the sync engine.
- Workers call
ChatRepository, never the DAO (the class with the database queries) or the API.
4. Make the hard part work (minutes 22 to 40)
In short: A send is a saved row first. A receive is a push hint that starts a sync.
4.1 The send path (minutes 22 to 33)
Your decision. The user taps send. What happens next, and when does the bubble appear?
First idea, and why it fails. Start a coroutine (a Kotlin background task) from the ViewModel, call the API, and add the bubble when the server answers. Three problems:
- The bubble waits for a round trip.
- If the user leaves the screen or the process stops, the call is cancelled.
- A plain retry after a lost response sends the message twice.
What I would say.
Say this. "A send is a row before it is a request. In one transaction, I insert the row as PENDING with a new clientId, so the bubble shows. A WorkManager worker sends it, one chain per conversation. Success fills in serverId and seq. Temporary failures retry with backoff (a longer wait each time). A permanent failure marks the row FAILED: tap to retry."
Those pending rows are my outbox: work that the server has not acknowledged. A transaction means all steps succeed or none do. I send through REST and WorkManager, not the socket, because they give retries. The code is in the build guide.
The follow-up question I expect: "The request reached the server, but the response never came back."
The retry carries the same clientId, and the server returns the original serverId and seq. This is idempotency: doing it twice has the same result as doing it once.
The follow-up after that: "What if the app is killed before the retry?"
- The outbox is rows in Room, so it survives the kill. On the next start, the worker finds the
PENDINGrow and sends it again. - The
clientIdis stored on that row, in the same transaction as the message. The retry reads it back. It never makes a new one. - Do not make the ID inside the worker. After a restart, a new ID looks like a new message. The server stores it twice, and the other person sees the message two times.
- Tap to retry sends the same
clientId, because the first try may have reached the server. Only a message that the server rejected for its content gets a new ID. - On logout, I keep or clear the outbox for that account. I never send it with the next user's token.
- Try both cases in the offline chat simulator: press Restart app, then turn on New ID on retry. To kill the app at each step of a send, use the relaunch explorer.
- App (UI + local database): Insert row: PENDING + clientId
- Outbox worker to Server: POST message (clientId)
- Server: Stores message once
- Server to Outbox worker: Ack lost in transit (lost)
- Outbox worker to Server: Retry POST (same clientId)
- Server to Outbox worker: 200: same serverId + seq
- Outbox worker to App (UI + local database): Mark SENT, store seq
The follow-up question I expect: "What do you retry?"
- IOException, 408, 429 and 5xx. Other 4xx errors are permanent.
- 401 is the exception. Refresh the token once, then retry.
- WorkManager backs off from 30 s by default. I set 10 s, its minimum (define work).
The follow-up question I expect: "And a message that always gets a 500?"
After eight attempts, the row becomes FAILED, so it cannot block that conversation's chain.
4.2 The receive path (minutes 33 to 40)
Your decision. The app is in the background and a message arrives. How does the phone find out?
First idea, and why it fails. Keep the socket open in a foreground service, or poll:
- Doze (the deep-sleep mode of an idle phone) stops network access, so a background socket is useless.
- A foreground service kept only for chat costs battery. It may also break Play rules for foreground services, so check the current policy.
- Polling wastes battery.
What I would say.
Say this. "While the app is on screen, I hold one WebSocket and upsert each event by clientId. In the background there is no socket. The server sends a high-priority push with only a hint: the conversation, and maybe the seq. The hint starts a sync worker. It fetches everything after my last seq, writes it to Room, then shows the notification. On iOS, the push is an alert push with a notification extension that fetches the message, and the sync still repairs any gap."
The follow-up question I expect: "Why not put the message in the push?"
A hint says only that something changed. A sync by cursor repairs itself, and a payload cannot.
The follow-up question I expect: "And when the socket drops?"
I reconnect with exponential backoff plus jitter (a random change in each delay). For example, wait 1 s, 2 s, 4 s, up to about 30 s, and randomize each wait. Without jitter, a server restart brings every client back at once. Then I run a cursor sync to fill the gap.
If I am cut off, I answer first. Then I say where I was:
Say this. "Yes, a duplicate sync is harmless, because every write is an upsert by clientId. I was in the middle of the receive path. I will finish it, then return to this."
4.3 Where iOS changes the design
The design is the same, but the guarantees are weaker. The Swift outbox is in the build guide.
- Database. SwiftData or Core Data is the source of truth. The screen reads it with
@Queryor@FetchRequest. A uniqueclientIdkeeps one row per message. - Sending. iOS has no WorkManager equivalent, so I send the outbox myself.
- I run a drain loop when the user sends, when the app returns to the foreground, when the network comes back and when a push arrives.
- For the request, I can write the body to a file and use a background URLSession upload task. The system can finish it while the app is suspended.
- For a short send,
beginBackgroundTaskasks the system for a little extra time. It does not promise how long. - After a force-quit, background transfers are cancelled and nothing runs until the next launch. Pending messages may wait. I say so.
- Receiving. There is no socket in the background. I close it when the app leaves the screen.
- Silent pushes (pushes with no visible alert) are low priority, not guaranteed, limited in number, and dropped after a force-quit (Apple).
- For chat, I send a visible alert push with
mutable-content: 1. A notification service extension (code iOS runs before a push shows) fetches the message and edits the text. - The extension has about 30 seconds. If time runs out, it shows generic text.
- When the user opens the app, a sync by cursor fills any gap.
5. Break it, ship it, roll it back (minutes 40 to 45)
In short: Each failure ends in a delivered message, a clear error, or a late notification, never a lost message.
| Failure | User sees | Data holds | Recovery |
|---|---|---|---|
| Process dies mid-write | The bubble was already there; it finishes sending | PENDING row, queued worker | The worker resumes |
| Lost acknowledgement | One bubble | clientId on the row | Same ID retried; the server returns the original seq |
| App killed after a lost acknowledgement | One bubble after the restart | PENDING row with its saved clientId | The worker resumes and sends the same ID; the server returns the original seq |
| Duplicate delivery | One bubble | Unique clientId index | Insert ignored, row updated |
| Long offline gap | Latest messages, a visible gap | Last seq per conversation | Cursor sync; for a large gap, fetch the latest page and mark it |
| Permanent rejection | "Tap to retry" | Status FAILED | Retry or delete |
| Background kill or throttle | A late notification, never a lost message | Work in WorkManager, not a Service or UI coroutine | The next sync catches up |
Rows not linked to a source are my design choices. Sources are in Further reading.
Releases:
- A Play staged rollout (a release to a growing share of users) can be halted. Users who have the build keep it (Play Console).
- An App Store phased release can be paused for up to 30 days (Apple). The page does not say anything about reverting installed copies.
- Old versions stay installed for months, so the server keeps accepting the old request.
- The new send path ships behind a remote flag (a setting the server can change). It is my kill switch, the only rollback that reaches installed apps.
- A schema migration (a database layout change) cannot be flagged off, so it ships one release early.
Say this. "I cannot recall a bad build, so the rollback is the flag plus a fixed build. I would watch tap-to-ack time, failed sends, duplicate rows and reconnects. I left out encryption, media and search."
6. Follow-ups that change a constraint
In short: Each follow-up changes one constraint. A strong answer says what breaks.
Practice prompts, not a claim about what any interviewer asks.
- Sixty messages queued offline, then signal returns. Strong: the bubbles stay in order (pending rows sort by creation time). The chain sends one by one (about 18 s, from my assumptions above). A 429 means slow down. Weak: "WorkManager handles it."
- Add photos and video. Strong: the message row stays small. The file is a separate resumable upload, as in photo and file sync, and the message follows. Weak: bytes in the send request.
- A second phone. Strong: each device has its own cursor, and the server decides the order. The new phone first loads past history. Unread counts come from a read cursor on the server. Weak: copying the database file.
- End-to-end encryption. Strong: each device has its own keys. Name what breaks: search on the server and push previews written by the server. My push is already only a hint. Weak: "just encrypt the body."
- A send-path bug is live for some users. Strong: switch the flag to the old path, pause the staged release, read the failed-send and duplicate-row rates, and ship a fix. Weak: "roll back."
- The backend wants to change the acknowledgement response. Strong: old versions stay in use for months, so the server answers in both shapes. Weak: assuming that everyone updates.
- The unread badge must match on two phones. Strong: the server keeps a read cursor per user and conversation. Each phone shows unread as messages after that cursor. Weak: counting unread rows on each phone.
- The socket token expires while connected. Strong: refresh the token before it expires, reconnect with the new one, then run a cursor sync. The send path is unaffected, because sends use REST with the same refresh rule. Weak: leaving the socket open with an old token. Try it: the token refresh explorer.
7. What changes by level
In short: As I read it, the levels differ in who raises the problem: the interviewer, the candidate, or the candidate with a rollout plan.
These levels are my own reading of public prep material, not any company's rubric.
- Mid-level: Room as the source of truth, a send flow with retry, basic offline handling. I want to hear: "A send is a row before it is a request."
- Senior: idempotency via
clientId, ordering by the server'sseq, FCM as a hint, backoff with jitter, and Doze raised unasked. I want to hear: "If the response is lost, the retry carries the sameclientId, so the server returns the originalseq." - Staff: multi-device sync, what encryption does to sync and search, delivery-time metrics, rollout behind flags, and a module plan (staff guide). I want to hear: "Here is what I would watch, and here is the flag that undoes it."
Common weak answers. These repeat across public prep material. They are not a dataset.
- Ten minutes on load balancers for a phone question.
- Only the normal case, with no failure behavior.
- Naming Room or WorkManager without saying what breaks otherwise.
- No client ID, so a retry makes doubles.
- No kill switch.
Staff answer: the decision and the rollout
- Decision: split the feature into send, sync and render modules, with one owner each. Keep the contract between them small:
clientId,seq, and a status that only moves forward. - Alternative: one module that every team edits. It is faster for a quarter, then every change crosses teams.
- Revisit trigger: if two teams keep changing the same module, the boundary is wrong.
- Rollout: the new send path goes behind the section 5 flag, then a staged release.
- Guardrail: send success must not drop against the previous version.
What I read and what is my view: I read the WorkManager, FCM, APNs (Apple's push service) and store-rollout facts in official docs. The module split, metrics, rollback plan, numbers and level text are my opinion. For shared code versus modules, see the native vs cross-platform guide.
Test yourself
In short: Seven questions from this case. Answer each one out loud first, then open the answer.
1. Why is a send a saved row before it is a request?
The row is saved as PENDING in one transaction, so the bubble shows at once. It also survives if Android stops the app. A worker sends it later and retries. A request started from the ViewModel is cancelled when the screen goes away, and a plain retry can send the message twice.
2. The same message arrives by socket, by a push-triggered sync and in the reply to your own send. How many rows do you store?
One. Each message gets a clientId (a UUID made on the device). A unique index refuses a second row, and the code does an upsert: insert, or update if the row exists.
3. What decides the order of messages?
The seq, a sequence number the server gives in each conversation. Device clocks never set the order. A pending message stays at the bottom until its ack brings a seq.
4. Why does the push carry only a hint and not the message?
A hint starts a sync by cursor, and a sync repairs itself. A payload cannot. FCM also limits a payload to 4 KB, and it can merge or drop queued messages. On iOS an alert push shows text, but a sync by cursor still repairs any gap.
5. The request reached the server, but the reply never came back. What happens on the retry?
The retry carries the same clientId. The server returns the original serverId and seq, so there is one message. This is idempotency: doing it twice has the same result as doing it once.
6. A bad send-path build is live. How do you roll back?
You cannot recall a build from phones. You switch a remote flag back to the old path, pause the staged release, and ship a fixed build. The flag is the only rollback that reaches installed apps.
7. On iOS, why do you send a visible alert push for chat and not a silent push?
Silent pushes are low priority, not guaranteed, limited in number, and dropped after a force-quit. So I send an alert push with mutable-content: 1. A notification service extension runs before it shows, fetches the message and edits the text.
Further reading (official docs)
- Build it: a chat app on Android and iOS: schema, REST, events, code and platform limits for this case
- Guide to app architecture (Android)
- Offline-first guidance (Android)
- Task scheduling and WorkManager (Android)
- Doze and App Standby (Android)
- Pushing background updates to your app (Apple)
Words on this page (32)
- Acknowledgement
- A short reply that says the server got a message. If this reply is lost, the app does not know, so it sends the message again.
- APNs
- Apple Push Notification service, the push service from Apple. Every push to an iPhone goes through it.
- Cold start
- A start of the app when it was not running. It is slower than a start from the background.
- Coroutine
- A light background task in Kotlin. It can pause and continue later without blocking a thread.
- Cursor
- A saved place in a list. The server gives it to the app, and the app sends it back to get the next part.
- DAO
- A class that holds the database queries. DAO means data access object.
- Doze
- The deep-sleep mode of an idle Android phone. It pauses network access and delays background work.
- Exponential backoff
- Waiting longer after each failure. For example, wait 1 second, then 2, then 4.
- FCM
- Firebase Cloud Messaging, the push service from Google. On iOS it passes the push to APNs.
- Foreground service
- An Android service that shows a notification while it works. The system is less likely to stop it.
- Idempotent
- Doing it twice has the same result as doing it once. This makes a retry safe.
- Jitter
- A random change in each wait time. It stops many apps from trying again at the same moment.
- Kill switch
- A remote flag that turns a feature off at once. It is the one rollback that reaches apps that are already installed.
- Outbox
- A saved list of changes that the server has not confirmed yet. The app sends them later, even after it restarts.
- Polling
- Asking the server again and again if there is news. It costs battery and data.
- Process death
- The system ends the process of your app to free memory. Anything that is only in memory is lost.
- Remote flag
- A setting that the server can change. It can turn a feature on or off without a new release.
- Repository
- The class that reads and writes data for the rest of the app. It hides whether the data comes from the network or the database.
- Resumable upload
- An upload that can continue after a break. It does not start again from the first byte.
- Rollback
- Going back to the old behavior after a problem. A build on a user's phone cannot be taken back, so teams use a kill switch.
- Room
- Android's library for the database on the phone. You use a DAO to read and write the data.
- Schema migration
- A change to the layout of a database. It must run safely when the user updates the app.
- Sequence number
- A number from the server that only goes up in each conversation. It sets the order. Here a gap is allowed, because a sync by cursor fills it.
- Silent push
- A push with no visible alert. The system can delay it or drop it, so treat it only as a hint.
- Source of truth
- The one copy of the data that the app trusts. In these cases it is the database on the phone, and the screen only reads it.
- Staged rollout
- A release to a growing share of users, not to everyone at once. It limits the harm of a bad release.
- Unique index
- A database rule that refuses a repeated value. It stops a second copy of a row from being saved.
- Upsert
- A database step that inserts a row, or updates it if it already exists.
- UUID
- A random unique ID. Two devices almost never make the same one.
- ViewModel
- A class that holds the state of one screen. It survives a screen rotation.
- WebSocket
- A two-way connection that stays open. The server can send data to the app at any time.
- WorkManager
- Android's tool for background work that must run later. The work survives when the app closes or the phone restarts.
What I checked and what I did not
- I read the Android pages for ViewModel, saved state, WorkManager, Doze, foreground-service limits and FCM. They support the 4 KB push limit, work that survives restarts, and queued FCM messages that can be merged or dropped.
- I read the Apple page on background updates. It says silent pushes are low priority, not guaranteed and throttled, and a held notification is dropped after a force-quit.
- I read the Play and App Store rollout help. Neither says an installed build can be taken back. A phased App Store release can be paused for up to 30 days.
- Doze suspends network access. The page does not mention sockets, so "a background socket is useless" is my reasoning.
- The numbers (300 bytes, 60 KB, 22 MB, 300 ms, 21 and 42 minutes) are my own assumptions and arithmetic. I rechecked the sums. The 42 minutes is an order of magnitude, because WorkManager caps backoff at 5 hours and real delays differ.
- I did not verify the 408 retry rule. It is my choice.
- I did not verify that iOS has no WorkManager equivalent beyond the background task APIs.
- No iOS engineer has reviewed the iOS parts yet.
- The follow-up questions and level descriptions are my reading of public prep material. They are not real transcripts or a company rubric.
Questions or corrections?
Add them to the discussion thread for this case on GitHub. To post, you need a free GitHub account. What you write there is public and follows GitHub's terms. Good answers are added to this page.
Keep reading
Get the free one-page cheat sheet
One page: the five steps, a 45-minute time plan and two checklists. Print it or keep it on your phone.
We email you the cheat sheet after you confirm your address. You can also open it now from the Start here page. You also get a short email each week with a follow-up round, and a note when a new case is published. Unsubscribe any time. Privacy.