Debug the Tape Cleaner
Problem statement
The desk runs clean_tape over the raw market-data tape before any strategy reads it. It has been in production for months. Recently the traders have started reporting occasional bad prints slipping through, stale fills appearing at the wrong time, the odd zero or negative price reaching the book, and a few rows that simply look wrong. Most of the time the output looks fine, which is why nobody caught it sooner. Your job is to find out what the cleaner is getting wrong and fix it.
The Job
clean_tape(prices) already ships (it is in the editor). It is supposed to satisfy the spec below exactly. It does not. Diagnose where it diverges and repair it so that it meets the contract for every input, not just the easy ones.
The Contract
Given a raw price tape, clean_tape must return a cleaned table that obeys all of these rules, in this order:
- Sort the rows ascending by
timestamp. - Drop duplicate timestamps, keeping the first occurrence after sorting.
- Treat any price that is zero or negative as missing.
- Forward-fill each asset column from its last valid (post-sort) price.
- A leading missing value with no prior valid price stays null (it cannot be filled).
Forward-fill means: if a price is missing, carry forward the most recent valid price for that asset. Each asset column is filled independently.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
prices
Raw tape rows in arbitrary table order; source_row preserves arrival order.
| Column | Type | Nullable | Description |
|---|---|---|---|
| source_rowPK | Integer | No | Unique arrival-order position in the raw tape. |
| timestamp | Integer | No | Milliseconds since the session opened. |
| asset_1 | Decimal | Yes | — |
| asset_2 | Decimal | Yes | — |
| asset_3 | Decimal | Yes | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| row_index | Integer | No | — |
| timestamp | Integer | No | — |
| asset_1 | Decimal | Yes | — |
| asset_2 | Decimal | Yes | — |
| asset_3 | Decimal | Yes | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- Input Format:
pricesis apd.DataFramewith these columns. timestamp—int— Milliseconds since session open.- FastPrep execution-adapter details (not visible in the source image): The input contains between
1and2000rows. source_rowvalues are unique positive integers and preserve source arrival order when timestamps tie.- Each asset value is an exact decimal or
NULL. - The returned columns are
row_index,timestamp,asset_1,asset_2, andasset_3.