skip to content

In a dating app's mobile chat, where should a just-sent message sit in the conversation before and after the server acknowledges it?

level: middleimportance: should knowfreq 58%

answer

  1. two kinds of rows on one screen
  2. acknowledged rows sort by sequence
  3. pending rows below, in typed order
  4. the phone's clock is display only
  5. one conversation sends one at a time

basics

~20 s

Until acknowledged, the local echo sits below every acknowledged message, in typed order. The acknowledgement gives it the server's sequence number, and from then on it sorts by that number like any other message, never by the phone's clock.

solid answer

~40 s

The screen sorts two kinds of rows. Acknowledged messages sort by the server's per-conversation **sequence number**, the one order both people see. Pending local echoes have no sequence yet, so they sit after all acknowledged messages, ordered by a local compose counter. When an acknowledgement arrives the echo takes its sequence and settles there; usually it stays put, but if the match's message got an earlier number it now sits above, and the list moves as little as possible. The phone's clock is used only to print times, because it can be wrong, two phones disagree, and quick messages can share a timestamp. To keep the user's own messages in the order typed, the client sends one conversation's pending messages one at a time, oldest first.

go deeper

for a junior

Remember the two kinds of rows: acknowledged messages sorted by the server's number, and your own pending messages at the bottom in the order you typed them.

for a middle

Explain why the phone's clock fails as a sort key with concrete cases, and why one conversation's pending messages are sent one at a time.

for a senior

Handle the race where both people send at once and the echo moves on acknowledgement, plus a failed message retried later, while keeping scroll position stable.

for a principal

Decide the product rule when an early message in a sequence fails: block later ones to keep order, or send on and accept a visible gap. Tie it to what users of this app expect.

## Two kinds of rows A dating app's chat screen shows two kinds of message at once: - **Acknowledged messages** — the server has stored them and given each a **sequence number**, a counter that increases by one per message in this conversation. Both people's phones receive the same numbers, so sorting by them shows both people the same conversation. - **Pending local echoes** — the user's own messages that are written on the phone and shown immediately, but not yet acknowledged. They have a client-generated id and no sequence number. The ordering rule must cover both, and must not change its mind more than necessary as acknowledgements arrive. ## The rule 1. Sort acknowledged messages by sequence number, ascending. 2. Place every pending echo **after** all acknowledged messages. 3. Order pending echoes among themselves by a local compose counter — a number the app increments each time the user taps send — not by a timestamp. 4. When an acknowledgement arrives, store its sequence number on the echo's row; from then on rule 1 places it. 5. Print the time next to each bubble from the server's timestamp once known, and from the phone's clock only while pending. Most of the time step 4 moves nothing: the user's message gets the next number, and it was already at the bottom. The interesting case is a race. The user taps send at the same moment the match's "Are you free Saturday?" arrives. If the server numbered the match's message 41 and the user's 42, the echo stays below. If the server took the user's first, the echo becomes 41 and moves above the match's 42 — exactly what the match sees, which is the point. The client animates that move quietly and keeps the scroll anchored at the bottom so the reader does not lose their place. ## Why not the phone's clock Sorting everything by "when it was written" sounds natural and is the classic mistake: | Problem with the sender's clock | Effect in a chat | |---|---| | A phone's clock can be set wrong by minutes or hours | Its messages jump above replies they answer | | Clocks can move backwards after a correction | A later message sorts before an earlier one | | Two phones' clocks disagree | The two people see different orders of the same conversation | | Two quick messages can share a timestamp | Their order is left to chance | The server's sequence avoids all four because one party assigns it. Time stays useful for display — "Saturday 21:14" — but it is never the sort key. ## Keeping the user's own messages in typed order The user who writes three messages in a row on weak signal expects them to arrive in that order. If the app fires all three in parallel, the second may reach the server first and be numbered first, and both people see the lines shuffled. So the client sends one conversation's pending messages **one at a time, oldest first**: the next leaves only after the previous one is acknowledged or has failed. Different conversations can still send in parallel, because order matters only within a conversation. If one message fails permanently, the client must decide whether later ones wait for the user's retry — waiting keeps order, sending on keeps the chat moving; this scenario sends on, and the failed bubble stays in the unsent zone at the bottom with tap-to-retry. The general rules for replaying an offline queue in order belong to the app's durable outbox; this is the chat-specific consequence. ## Edge cases interviewers raise - **History loaded above the echo.** Scrolling up fetches older acknowledged messages; they sort by sequence above everything, never disturbing pending rows. - **An echo that fails.** It keeps its place among pending rows, marked failed; a retry reuses the client id and, once acknowledged, it takes whatever sequence the server now assigns — possibly well after messages that arrived while it waited. - **The same message arriving again.** The echoed copy from the server carries the client id, so it merges into the pending row instead of appearing a second time. A strong answer states the sort key, the pending zone, the race, and why the clock is display-only — in that order.

  • A failed message is retried ten minutes later; where does it end up after acknowledgement?
    Wherever its new sequence number puts it, which is after everything the server stored in those ten minutes. The client should not try to slot it back to where it was typed; both people see the server's order. Showing its original compose time beside it explains the gap.
  • Why not let the server reorder pending messages by a timestamp the client sends?
    Because the timestamp comes from the phone's clock, which can be wrong or move backwards; the server would be sorting by the weakest input it has. Sending one at a time per conversation gives the server the user's order without trusting any clock.

  • Sort the whole conversation by each sender's device timestamp.
  • A message cannot be shown until the server returns its sequence number.
  • Pending messages sent in parallel still arrive in the order typed.
  • A time-ordered client id is a safe sort key for the conversation.