Longest Transcript Window Under Token Budget and Tool Limit

Last updated: November 29, 2026

Quick Overview

Each conversation turn has a token count and a flag saying whether it is a tool call. Find the longest contiguous run of turns whose total tokens fit in a budget and that contains at most k tool calls, returning its length and start index.

Sierra
Coding & Algorithms
Software Engineer
Sierra
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Each conversation turn has a token count and a flag saying whether it is a tool call. Find the longest contiguous run of turns whose total tokens fit in a budget and that contains at most k tool calls, returning its length and start index.

When Sierra builds context for a model call it has to fit recent conversation history into a token budget while limiting noisy tool output. The interviewer wants a correct two-constraint sliding window and a clear argument for why the window can only move forward.

What the Interviewer Expects
  • Recognize that positive token counts make the window monotone
  • Maintain both the token sum and tool count incrementally
  • Shrink from the left until both constraints hold
  • Return the earliest window on ties and handle the no-valid-window case
  • State O(n) time and O(1) extra space
Key Topics to Cover
sliding window
two pointers
multiple constraints
context window management
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What breaks if some turns can have zero or negative weight?
  • How would you require the window to always include the latest turn?
  • How would you pick a non-contiguous subset of turns maximizing a relevance score under the budget?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Inputs: tokens[i] > 0, is_tool[i] in {0, 1}, a budget, and k. Output: (length, start) of the longest contiguous window with sum of tokens at most budget and tool count at most k. If even...

High-Level Design

Both constraints are monotone: removing a turn from the left can only lower the sum and the tool count. So for each right end, the smallest valid left end never moves backward as right advances. That ...


Submit Your Answer
Markdown supported

Related Questions