Problem · Database

Clean the Tape

MediumMillennium logoMillenniumNEW GRADINTERNOA

Problem statement

The prices table contains a market-data tape for three assets. Return a cleaned tape with the columns row_index, timestamp, Asset_1, Asset_2, and Asset_3.

Cleaning rules

  1. Remove rows that are exact duplicates across timestamp and all three asset columns.
  2. Sort the remaining rows by timestamp in ascending order.
  3. Treat every asset value less than or equal to 0 as missing.
  4. For each asset independently, forward-fill a missing value from that asset's most recent positive value in the sorted tape.
  5. Never backfill. If an asset has no earlier positive value, keep the output value NULL.
  6. Reset the row index after sorting so that row_index is 0, 1, and so on.

Table schema

MySQLPostgreSQLPandas

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 price-tape rows with timezone-neutral whole-second timestamps. Column order is timestamp, Asset_1, Asset_2, Asset_3.

ColumnTypeNullableDescription
timestampTimestampNoTimezone-neutral observation time in whole-second YYYY-MM-DD HH:MM:SS precision.
Asset_1DecimalYesObserved price for Asset_1.
Asset_2DecimalYesObserved price for Asset_2.
Asset_3DecimalYesObserved price for Asset_3.

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
row_indexIntegerNoZero-based index after cleaning and sorting.
timestampTimestampNo
Asset_1DecimalYes
Asset_2DecimalYes
Asset_3DecimalYes

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • The input contains at least 1 row and at most 2000 rows.
  • Every timestamp is timezone-neutral and uses whole-second YYYY-MM-DD HH:MM:SS precision.
  • After exact duplicate rows are removed, no two remaining rows share the same timestamp.
  • Each asset value is a decimal or NULL.
  • An exact duplicate may occur more than once and must contribute only one output row.
  • Return rows in ascending timestamp order with the exact result-column order shown.

More Millennium problems