Paginated Message Fetch with a Token-Budget FIFO Window and Property-Based Dedup

Quick Overview

A three-part coding exercise on the message layer of a conversational agent: fetch every message from a cursor-paginated API, keep a FIFO context window within a fixed token budget, then enforce uniqueness on a chosen message property. It tests pagination loops, queue-based eviction and set-based deduplication.

Paginated Message Fetch with a Token-Budget FIFO Window and Property-Based Dedup

Company: Sierra

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

You are building the message layer for a conversational AI agent. A client library returns a conversation's messages one page at a time, using a pagination cursor. The exercise has three parts, and each part extends the function from the previous one. Assume an interface like this (names may differ in the interview): ```python class Message: id: str content: str # messages may carry further properties, such as a sender or a thread id class Page: messages: list[Message] next_cursor: str | None # None when there are no more pages class MessageClient: def get_messages(self, cursor: str | None = None) -> Page: ... ``` ### Constraints and Clarifications - The first request passes no cursor. Every later request passes the cursor returned with the previous page. - A helper `count_tokens(message) -> int` returns a message's token count, which is never negative. - Unless the interviewer says otherwise, pages arrive oldest first, and messages within a page are oldest first. ### Clarifying Questions - How many messages can one conversation hold, and is it acceptable to keep all of them in memory at once? - Can `get_messages` fail with a timeout or a rate-limit error, and should the function retry? - Can a page be empty while still returning a cursor? ### Part 1: Cursor-based message retrieval Implement `fetch_all_messages(client) -> list[Message]`. Request messages from the client using the pagination cursor, iterate through each page until no further cursor is returned, and aggregate all messages into a single list in the order the API returned them. ```hint Where the loop stops Decide what ends the loop, and check that the messages on the final page are still collected. ``` #### What This Part Should Cover - Correct termination when no cursor comes back, including on the very first page. - Message order preserved across pages. - Protection against a cursor that repeats and against transient client errors. ### Part 2: Token-budgeted context window Extend retrieval to keep the messages inside a fixed token budget, managed as a FIFO cache. Implement `fetch_context(client, token_budget, count_tokens) -> list[Message]`: - Track the token count of each incoming message. - Maintain a running total of the tokens held in the cache. - When the total exceeds the budget, evict the oldest messages until it fits again. - Return the messages that remain, oldest first. They form the context passed to the model. ```hint Cheap at both ends Pick a structure where adding the newest message and removing the oldest are both constant time, and keep the running total in step with it. ``` #### Clarifying Questions for this Part - Must the total stay within the budget after every insertion, or only in the returned result? - What should happen to a single message whose token count is larger than the whole budget? #### What This Part Should Cover - An eviction structure with constant-time append and evict, and a running total that always matches its contents. - The invariant that the result fits the budget and is the most recent run of messages, in order. - The policy for a message larger than the budget, and for a budget of zero. - Memory use when the conversation is far larger than the budget. ### Part 3: Uniqueness by a message property Extend the previous function so that uniqueness is enforced on one message property, named by the caller as `property_name`. Track the property values already encountered, handle duplicate messages according to the uniqueness constraint, and keep the message collection consistent with the token budget from Part 2. ```hint Duplicates in the middle The earlier copy of a duplicate may sit anywhere in the window. Decide what each structure must record so that the duplicate check, the eviction and the token total stay consistent with each other. ``` #### Clarifying Questions for this Part - When a duplicate arrives, should the window keep the first copy or replace it with the newer one? - After a message is evicted for the budget, may a later message with the same property value enter again? - How should messages that lack the property be treated? #### What This Part Should Cover - A stated duplicate policy, applied consistently with eviction. - Tracking of seen values that stays in sync with the window, and its memory cost. - Correct token totals when a duplicate is skipped or replaces an older copy. ### What a Strong Answer Covers - Each part built as an extension of the previous one, with shared code for paging and eviction. - Explicit invariants for order, budget and uniqueness, checked by tests. - Time linear in the number of messages, and memory bounded by the window where possible. - Stated policies for the cases the requirements leave open: oversized messages, duplicate handling and re-entry after eviction. ### Follow-up Questions - The API returns the newest messages first. How do your loop and your eviction change, and can you stop fetching early? - New messages keep arriving during a live conversation. How would you update the window without fetching everything again? - Token counting calls a slow tokenizer. How would you avoid counting the same message twice across calls? - The system prompt and the first user message must always stay in the context, whatever their age. How does the eviction change?

Overview: A three-part coding exercise on the message layer of a conversational agent: fetch every message from a cursor-paginated API, keep a FIFO context window within a fixed token budget, then enforce uniqueness on a chosen message property. It tests pagination loops, queue-based eviction and set-based deduplication.

Read the full Sierra Software Engineer interview experience this question came from

Sierra logo
Sierra
Nov 8, 2026
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
2
0

You are building the message layer for a conversational AI agent. A client library returns a conversation's messages one page at a time, using a pagination cursor. The exercise has three parts, and each part extends the function from the previous one.

Assume an interface like this (names may differ in the interview):

class Message:
    id: str
    content: str
    # messages may carry further properties, such as a sender or a thread id


class Page:
    messages: list[Message]
    next_cursor: str | None  # None when there are no more pages


class MessageClient:
    def get_messages(self, cursor: str | None = None) -> Page:
        ...

Constraints and Clarifications

  • The first request passes no cursor. Every later request passes the cursor returned with the previous page.
  • A helper count_tokens(message) -> int returns a message's token count, which is never negative.
  • Unless the interviewer says otherwise, pages arrive oldest first, and messages within a page are oldest first.

Clarifying Questions Guidance

  • How many messages can one conversation hold, and is it acceptable to keep all of them in memory at once?
  • Can get_messages fail with a timeout or a rate-limit error, and should the function retry?
  • Can a page be empty while still returning a cursor?

Part 1: Cursor-based message retrieval

Implement fetch_all_messages(client) -> list[Message]. Request messages from the client using the pagination cursor, iterate through each page until no further cursor is returned, and aggregate all messages into a single list in the order the API returned them.

What This Part Should Cover Guidance

  • Correct termination when no cursor comes back, including on the very first page.
  • Message order preserved across pages.
  • Protection against a cursor that repeats and against transient client errors.

Part 2: Token-budgeted context window

Extend retrieval to keep the messages inside a fixed token budget, managed as a FIFO cache. Implement fetch_context(client, token_budget, count_tokens) -> list[Message]:

  • Track the token count of each incoming message.
  • Maintain a running total of the tokens held in the cache.
  • When the total exceeds the budget, evict the oldest messages until it fits again.
  • Return the messages that remain, oldest first. They form the context passed to the model.

Clarifying Questions for this Part Guidance

  • Must the total stay within the budget after every insertion, or only in the returned result?
  • What should happen to a single message whose token count is larger than the whole budget?

What This Part Should Cover Guidance

  • An eviction structure with constant-time append and evict, and a running total that always matches its contents.
  • The invariant that the result fits the budget and is the most recent run of messages, in order.
  • The policy for a message larger than the budget, and for a budget of zero.
  • Memory use when the conversation is far larger than the budget.

Part 3: Uniqueness by a message property

Extend the previous function so that uniqueness is enforced on one message property, named by the caller as property_name. Track the property values already encountered, handle duplicate messages according to the uniqueness constraint, and keep the message collection consistent with the token budget from Part 2.

Clarifying Questions for this Part Guidance

  • When a duplicate arrives, should the window keep the first copy or replace it with the newer one?
  • After a message is evicted for the budget, may a later message with the same property value enter again?
  • How should messages that lack the property be treated?

What This Part Should Cover Guidance

  • A stated duplicate policy, applied consistently with eviction.
  • Tracking of seen values that stays in sync with the window, and its memory cost.
  • Correct token totals when a duplicate is skipped or replaces an older copy.

What a Strong Answer Covers Guidance

  • Each part built as an extension of the previous one, with shared code for paging and eviction.
  • Explicit invariants for order, budget and uniqueness, checked by tests.
  • Time linear in the number of messages, and memory bounded by the window where possible.
  • Stated policies for the cases the requirements leave open: oversized messages, duplicate handling and re-entry after eviction.

Follow-up Questions Guidance

  • The API returns the newest messages first. How do your loop and your eviction change, and can you stop fetching early?
  • New messages keep arriving during a live conversation. How would you update the window without fetching everything again?
  • Token counting calls a slow tokenizer. How would you avoid counting the same message twice across calls?
  • The system prompt and the first user message must always stay in the context, whatever their age. How does the eviction change?