AO
Back

Conversation History: Paginate, Then Compact by Tokens

Phone ScreenPhone ScreenLast reported November 2026Medium Frequency

Problem Overview

The round: a 45-minute phone screen, reported four times from June to November 2026, for agent engineering and new-grad roles. One new-grad report says AI tools were not allowed. You are handed a small API with several classes and asked to work with a conversation's history. Three reports say the code is not hard and reading the API is the hard part: one candidate spent a long time just understanding the prompt, another advises working out how the classes relate in the first few minutes. In one report the screen opened with a project discussion that left about 30 minutes for code. The round has three parts; the first two are known and are the two graded parts here. No report gives the third part's task or rules (see the note at the end).

Given — what the tests call

Four classes, in the editor above your code. Read them first.

class Usage:              # usage.token_count: int
class Message:            # message_id, role, text, usage (a Usage)
class Page:               # messages: list[Message], NEWEST first; next_cursor: str | None
class ConversationClient:
    def __init__(self, conversations: dict, page_size: int = 50): ...
    def list_messages(self, conversation_id: str, cursor: str | None = None) -> Page: ...
  • list_messages(conversation_id) with no cursor returns the first page: the newest messages. Pass page.next_cursor back to get the next, older page. The last page has next_cursor None.
  • Passing None again starts over from the newest page. Cursors are opaque strings.
  • The tests build the client as ConversationClient(conversations, page_size=2), where conversations maps an id to its messages, oldest first. Your code only calls list_messages.
  • An unknown conversation id is an empty conversation: one page with no messages.
  • client.calls counts calls to list_messages. The tests check it.

Part 1 — page through a conversation

def fetch_history(client, conversation_id: str) -> list[Message]: ...
  • Keep calling list_messages with each page's next_cursor until there is no next page.
  • Return every Message, oldest first.
  • Make one call per page and no more.

Part 2 — compact the history by token count

def compact(messages: list[Message], trigger: int = 20000, target: int = 10000) -> list[Message]: ...
  • Each message has a token count in message.usage.token_count.
  • If the total is above trigger (20,000), drop the oldest messages until the total is under target (10,000). Return the messages left, oldest first.
  • If the total is 20,000 or less, return all of them. A history between 10,000 and 20,000 is left alone.
  • Return a new list; do not change the one passed in. A target above trigger raises ValueError.

Not reported: the third part. No report gives its task or rules. Two replies say it is close to Part 2 and uses the same idea. Nobody in these reports says they finished it: two ran out of time after Part 2, one was asked to explain the approach with 10 minutes left, and another explained it along with how to optimize the second and third parts together.

Notes: the class names, fields and method signature above are ours, written to match what the reports describe: pages linked by next_cursor, and a token_count on each message. So are newest-first pages, "None starts over", the oldest-first result, the strict "under 10,000" (exactly 10,000 drops one more) and the ValueError rules. The 20,000 and 10,000 thresholds and dropping the oldest messages first are reported. The given API in your round will differ, so read it before you code.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 1
Trade-off discussion

Explain how you would do the third part, and how the second and third parts could be optimized together.

Probes for: Reported; time ran out after Part 2

What Reports Emphasize

What the reports say: three reports say the code is easy and the given API is the hard part. Spend the first minutes mapping how the classes relate, then code. Part 2 uses two thresholds: compact above 20,000 tokens, down to under 10,000, dropping the oldest messages first. Nobody reported finishing the third part in 45 minutes, so move fast through the first two.

Common mistakes (ours, not reported): Stopping on an empty page instead of on the next cursor; one extra call after the last page; returning messages in page order when the pages are newest first; compacting whenever the total is above 10,000; re-summing the tokens after every dropped message.

Edge cases to test (ours): An empty conversation; a history that fills its last page exactly; a total between 10,000 and 20,000; a total that lands exactly on 10,000 while dropping; one message bigger than the target.

Practice

Write your own against 11 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Sierra AI · Phone Screen · Reported 4× across candidate reports
Is this helpful?