PIXELBANKv9.1.0
Menu

Trigger Summarization at a Budget Fraction

Problem Statement

An agent summarizes old turns once its running context grows past a fraction of the model's window. Given cumulative message token sizes, find the first message index at which summarization should trigger.

Background

Let budget be the context window and threshold a fraction (e.g. 0.75). Walking messages in order and accumulating tokens, summarization triggers at the first message whose inclusion makes the running total strictly exceed threshold * budget. If the total never exceeds it, no trigger fires.

Your Task

Implement:

def summarize_trigger(sizes, budget, threshold):
  • sizes: list of per-message token counts, in order.
  • Return the 0-based index of the first message at which the running sum > threshold * budget, or -1 if it never happens.

Input Format

  • sizes (list of ints), budget (int), threshold (float).

Output Format

  • An int index or -1.

Sample

print(summarize_trigger([100, 100, 100, 100], 500, 0.75))

Output:

3

Example:

Input:
print(summarize_trigger([100, 100, 100, 100], 500, 0.75))
Output:
3
Reasoning:
  • Calculate the token limit by multiplying the budget by the threshold: 500×0.75=375500 \times 0.75 = 375. This defines the strict upper bound for the running sum before summarization triggers.
  • Process the first message (index 0) with 100 tokens. The running sum becomes 0+100=1000 + 100 = 100. Since 100≯375100 \ngtr 375, the trigger condition is not met.
  • Process the second message (index 1) with 100 tokens. The running sum becomes 100+100=200100 + 100 = 200. Since 200≯375200 \ngtr 375, the trigger condition is not met.
  • Process the third message (index 2) with 100 tokens. The running sum becomes 200+100=300200 + 100 = 300. Since 300≯375300 \ngtr 375, the trigger condition is not met.
  • Process the fourth message (index 3) with 100 tokens. The running sum becomes 300+100=400300 + 100 = 400. Since 400>375400 > 375, the threshold is exceeded, and the function returns the current index.
  • The final output is 3

Constraints:

  • 0 <= len(sizes) <= 100000, budget >= 1, 0 < threshold <= 1.
  • Trigger is the first index where the running sum strictly exceeds threshold*budget.
  • Return -1 if never exceeded.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Trigger Summarization at a Budget Fraction - Medium | PixelBank