Compact Conversation History by Token Budget
Reported by candidates from Sierra's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Sierra OA reported in October 2026 looks like a freebie, and that's exactly why people lose points on it. Compact Conversation History by Token Budget asks for the longest suffix of messages whose token costs fit a budget. It's a suffix scan, not a real algorithm puzzle. The trap is in the edges: zero budgets, zero-cost messages, empty input, and sums that blow past 32-bit ints. If you've got an invite for this one, read the constraints twice. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank on the details, but this one is very learnable tonight.
The problem
You are given an ordered conversation history messages, a parallel array tokenCounts, and a non-negative context budget tokenBudget. The value tokenCounts[i] is the token cost of messages[i]. If the total token cost exceeds the budget, remove whole messages from the beginning until the retained total is at most tokenBudget. Return the longest suffix of messages that fits. Preserve the original order of every retained message. A total exactly equal to the budget fits. Function compactConversationHistory(messages: String[], tokenCounts: int[], tokenBudget: int) → String[] Examples Example 1 messages = ["system","user","assistant"] tokenCounts = [4,5,6] tokenBudget = 10 return = ["assistant"] The total is 15. Removing "system" leaves 11, so "user" must also be removed. The remaining cost is 6. Example 2 messages = ["a","b","c"] tokenCounts = [2,3,5] tokenBudget = 8 return = ["b","c"] Removing the oldest message lowers the total from 10 to exactly 8, so both remaining messages are retained. Example 3 messages = ["oversized"] tokenCounts = [12] tokenBudget = 0 return = [] The only message exceeds a zero budget, so the compacted history is empty. Constraints 0 <= messages.length <= 10^5 tokenCounts.length == messages.length Every entry in messages is a non-empty ASCII string. 0 <= tokenCounts[i] <= 10^9 0 <= tokenBudget <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk from the end of the array backward, keeping a running sum. Add tokenCounts[i] only if the sum stays at or below tokenBudget. The moment adding the next older message would exceed it, stop. Return messages from that index to the end, in original order. The pitfall is the naive approach: sum everything, then strip from the front. It works but invites off-by-one bugs, and people forget that a total exactly equal to the budget fits, so the comparison must be <=, not <. Another trap is overflow. With 10^5 messages at up to 10^9 each, the total reaches 10^14, so use a 64-bit integer. Also don't skip over an oversized message and keep going. The result must be a contiguous suffix. Zero-cost messages at the boundary get included naturally. If you blank on the stop condition during the live OA, StealthCoder can hand you the loop. Time is O(n), space is O(1) beyond the output.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Compact Conversation History by Token Budget cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Sierra's OA.
Sierra reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Compact Conversation History by Token Budget FAQ
What's the trick in the Sierra token budget problem?+
Scan from the newest message backward, accumulating token costs, and stop at the first message that would push the sum over the budget. The answer is the contiguous suffix from that point. Don't skip an oversized message and keep going, because the result has to be a suffix.
How hard is Compact Conversation History by Token Budget really?+
Easy on algorithm, annoying on edges. It's a single linear pass. Points get lost on exact-equals-budget handling, a zero budget, empty input, and integer overflow when summing up to 10^5 values of 10^9 each.
Do I need a 64-bit integer for the sum?+
Yes in languages with 32-bit ints. Total cost can reach about 10^14, well past the 32-bit limit. Even scanning backward with an early stop, one addition can overflow before your comparison runs, so use long or equivalent.
What should I return when nothing fits?+
Return an empty array. Example 3 shows it: one message costs 12 against a budget of 0, so nothing is retained. The same applies to empty input. Zero-cost messages still fit even with a zero budget, so don't special-case budget 0 incorrectly.
How do I prepare for this in 48 hours?+
Write the backward scan once from memory and test it on the three examples. Then add tests for exact-budget equality, a zero-cost message at the boundary, an empty list, and huge values. That covers nearly every way this question can go wrong.