cswork / libraryOpen practice workspace

Coding questions · Instacart

Pivot data without date libraries

This question evaluates a candidate's competency in data manipulation and transformation, specifically pivoting tall datasets into wide views, handling missing metric values, ordering ISO date strings via string operations, and meeting algorithmic efficiency constraints (O(n log n)).

Transform a tall dataset into a wide, pivoted view without using any date/time libraries. Input: a list of records (date, store_id, metric, value) where date is an ISO 8601 string YYYY-MM-DD, metric ∈ {"sales", "refunds", "visits"}, and value is a non-negative integer. Output: for each (store_id, date), produce one row with columns [store_id, date, sales, refunds, visits], filling missing metric values with 0 and ordering rows by ascending date within each store. Requirements: (1) Implement this in code (any language), but you may not import date/time packages; rely only on string operations for ordering dates. (2) Ensure the solution is O(n log n) or better with respect to the number of records n. (3) Explain how you would handle malformed dates, duplicate (store_id, date, metric) entries, and very large inputs that do not fit in memory.

Pivot tall store metrics into wide daily rows using only ISO date strings

You are given a tall table of store metrics with columns (date_str, store_id, metric, value). The date is stored as an ISO 8601 string in the format YYYY-MM-DD (not a DATE type). The metric is one of: 'sales', 'refunds', 'visits'.

Write a single SQL query that produces a wide, pivoted result with one row per (store_id, date_str) and columns: [store_id, date_str, sales, refunds, visits]

Rules/requirements:

  1. Do NOT cast date_str to a DATE type or use date/time functions. Rely on the ISO string format for ordering.
  2. Fill missing metrics with 0.
  3. If there are duplicate rows for the same (store_id, date_str, metric), aggregate them by summing value.
  4. Exclude malformed date strings. For this question, treat a date as valid only if:
    • it matches the pattern 'YYYY-MM-DD' (length 10 with '-' in positions 5 and 8),
    • month is between '01' and '12', and day is between '01' and '31' (string comparisons are acceptable).
  5. Order the final output by store_id ascending, then date_str ascending (which will also be chronological because of ISO formatting).

Table store_metric_events

columntypeconstraints
event_idINTPRIMARY KEY, NOT NULL
date_strVARCHAR(10)NOT NULL
store_idINTNOT NULL
metricVARCHAR(10)NOT NULL
valueINTNOT NULL

Sample data:

date_strevent_idmetricstore_idvalue
2025-05-301sales1100
2025-05-302refunds15
2025-05-303visits150
2025-05-314sales1120
2025-05-315sales130
2025-05-316visits160
2025-05-307visits240
2025-06-018sales2200
2025-06-019refunds23
2025-5-2910sales110
2025-13-0111visits210

Expected output

One row per (store_id, date_str) with pivoted metric columns. Missing metrics are 0. Duplicate (store_id, date_str, metric) values are summed. Rows with malformed date_str are excluded. Ordered by store_id then date_str ascending.

date_strrefundssalesstore_idvisits
2025-05-305100150
2025-05-310150160
2025-05-3000240
2025-06-01320020

Hints

  1. Because dates are in ISO format (YYYY-MM-DD), string sorting matches chronological sorting.
  2. Use conditional aggregation (SUM with CASE WHEN) to pivot the metric rows into columns.
  3. To handle duplicates for the same (store_id, date_str, metric), aggregate with SUM rather than MAX.