← Questions

Evict old conversations to cap the distinct listings in context

0
SierraCodingLast asked Oct 2026

An agent keeps a history of conversations, oldest first, and each conversation mentions some listing IDs (the same ID can show up in many conversations, or twice in one). To keep the context small, drop whole conversations from the oldest end until the remaining ones mention at most max_listings distinct IDs. Then return the distinct IDs that remain, in the order they first appear. Do it in one pass by keeping a count per ID, so an ID only stops counting once its last remaining mention is gone.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding