83. Fit Conversations into a Context Window
Difficulty: Hard · Topics: Data Prep for AI, Window Functions, Joins
Chat logs in messages (conv_id, turn, role, tokens) are turned into training examples for a model with a 1,000-token context window. Each conversation has one system message (turn 0); turn is unique within a conversation and larger is newer.
For every conversation keep the system message, then the newest messages that fit: walking back from the newest turn, keep a message while the system message plus every kept message add up to at most 1,000 tokens, and stop at the first one that does not fit (older turns are dropped even if they are small). Return the kept rows: conv_id, turn, role, tokens.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
messages
| conv_id | turn | role | tokens |
|---|---|---|---|
| 1 | 0 | system | 200 |
| 1 | 1 | user | 300 |
| 1 | 2 | assistant | 400 |
| 1 | 3 | user | 150 |
| 1 | 4 | assistant | 220 |
| 2 | 0 | system | 100 |
| 2 | 1 | user | 50 |
| 2 | 2 | assistant | 60 |
Expected output
| conv_id | turn | role | tokens |
|---|---|---|---|
| 1 | 2 | assistant | 400 |
| 1 | 3 | user | 150 |
| 1 | 4 | assistant | 220 |
| 2 | 1 | user | 50 |
| 2 | 2 | assistant | 60 |
| 1 | 0 | system | 200 |
| 2 | 0 | system | 100 |
Hints
Hint 1
Order each conversation newest first and take a running sum of tokens:Window.partitionBy("conv_id").orderBy(F.col("turn").desc()) with rowsBetween(Window.unboundedPreceding, Window.currentRow).Hint 2
The system message counts toward the budget but is not part of the running sum: join each conversation's system tokens to its other messages first.Hint 3
Because tokens are positive, the running sum only grows, so "keep while it fits" is simplyrunning + system <= 1000. Add the system rows back at the end with unionByName.Learn the concepts
- rangeBetween · 6 min read. Window frames by value instead of row count: the last 7 calendar days, not the last 7 rows.
- unionByName · 4 min read. Stack DataFrames by column name instead of position, and survive schema drift with allowMissingColumns.
- row_number / rank · 4 min read. Number and rank rows inside a window, and how the three ranking functions treat ties.
PySpark functions you'll practise
- Window spec
- Rows Between
- sum
- join
- filter
- select
- withColumn
- orderBy
- unionByName
Related problems
- Build Preference Pairs from Ratings · Hard · Data Prep for AI, Window Functions, Joins
- Remove Near-Duplicate Documents · Medium · Data Prep for AI, Window Functions, Strings
- Split Train and Test With No User in Both · Medium · Data Prep for AI, Joins, Aggregations
- Filter Low-Quality Training Text · Medium · Data Prep for AI, Strings, Null Handling
- Mask Personal Data Before Training · Easy · Data Prep for AI, Strings, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice