Fetch Cursor-Paginated Conversation History and Compact It by Token Count
Company: Sierra
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
In a 45-minute hands-on coding interview you work in a small existing codebase built around a conversation history. Several API classes return conversations and their content, and the first job is to work out how those classes relate to each other before extending them. The report describes three parts that had to be finished quickly; the first two are below, and the third is not described in the report.
Names other than `next_cursor` and `token_count` are placeholders; use whatever the provided code defines.
### Clarifying Questions
- Which class or method fetches a page, what does a page contain, and how is the end signalled: a missing `next_cursor`, an empty value, or a page with no conversations?
- Is the result a list of conversations, the messages of one conversation, or both, and in what order should it be returned?
- Where does `token_count` appear and in what form: a number on each message, or a value embedded in a string or nested metadata that has to be parsed?
- What does compacting mean here: dropping messages, merging them, or replacing older ones with a summary? Against what token limit?
- Which messages must always survive compaction, for example a system instruction or the latest user turn?
### Part 1 — Follow `next_cursor` to fetch every page
Using the provided API, retrieve the complete conversation history. Each response returns one page of results together with a `next_cursor`; keep requesting the next page with that cursor until there are no more conversations, and return everything in order.
```hint What ends the loop
Decide exactly which response tells you nothing is left, and what your loop does if the API ever hands back a cursor you have already used.
```
#### What This Part Should Cover
- Correct use of the existing classes and of the cursor contract, from the first request to the stopping condition
- Results that are complete and in order, with no duplicates and no lost final page
- Behaviour on transient errors and on a cursor that repeats or never ends
### Part 2 — Compact the conversation using `token_count`
Parse the `token_count` values of the conversation's messages and use them to compact the conversation so that its total fits a token budget. Agree the budget and the compaction rule with the interviewer.
```hint Count before you cut
Think about which messages a model continuing this conversation needs most, and what a missing or malformed `token_count` should do to your running total.
```
#### What This Part Should Cover
- Robust parsing of `token_count`, including missing or malformed values
- A clear compaction rule that respects the budget and keeps the messages that must survive
- Message order and roles preserved in the result, plus the cases of an empty conversation and a single message larger than the budget
### What a Strong Answer Covers
- Reads the provided code first and states how its API classes relate before writing anything
- Reuses the existing classes and types instead of rebuilding them
- A working pagination loop and a budget-respecting compaction, each with its cost stated
- Edge cases across both parts, checked with quick tests against the provided API
- Both parts working within the time box, in code clean enough to extend for the next part
### Follow-up Questions
- The history is too large to hold in memory. How would you change the fetch so that compaction works on a stream of pages?
- How would you replace dropped messages with a summary, and how do you account for the summary's own tokens?
- The API rate-limits you or fails partway through the pages. How do you resume without starting over?
- How would you test the compaction rule?
Overview: A hands-on coding task in an existing codebase: fetch a complete conversation history by following next_cursor across pages, then parse token_count values and compact the conversation to fit a token budget. It tests reading unfamiliar API classes quickly, pagination edge cases and budget-aware trimming.
Read the full Sierra Software Engineer interview experience this question came from