Attach the Latest Prior Carrier Status to Each Package Scan

Last updated: November 29, 2026

Quick Overview

You have internal package scans (package_id, scan_ts, facility) and an external carrier status feed (package_id, status_ts, carrier_status). In pandas, attach to every scan the most recent carrier status for the same package at or before the scan time, ignoring statuses more than six hours old.

Amazon
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


You have internal package scans (package_id, scan_ts, facility) and an external carrier status feed (package_id, status_ts, carrier_status). In pandas, attach to every scan the most recent carrier status for the same package at or before the scan time, ignoring statuses more than six hours old.

Amazon Logistics reconciles its own scans with partner carrier feeds to find where packages stall and which carrier data is stale. The interviewer wants to see an as-of join instead of a cross join plus filter, and attention to sort requirements, tolerances and timezones.

What the Interviewer Expects
  • Recognize the as-of (point-in-time) join pattern and use pd.merge_asof
  • Sort both frames on the time key and use by= for the package
  • Set direction='backward' and a tolerance so stale statuses are not attached
  • Parse both timestamps into the same timezone before joining
  • Explain the exact-match rule (same timestamp counts as prior)
Key Topics to Cover
merge_asof
point-in-time join
time series
tolerance
timezones
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you write the same point-in-time join in SQL?
  • What if the carrier feed sometimes sends statuses out of order or with duplicate timestamps?
  • How would you find packages whose carrier status lags Amazon scans by more than a day on average?
  • How does memory scale if the carrier feed has billions of rows?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data
  • scans(package_id, scan_ts, facility) from Amazon sort centers and delivery stations.
  • carrier(package_id, status_ts, carrier_status) from a partner carrier feed, timestamps possibly in a differ...
Approach

A naive approach merges on package_id, filters status_ts <= scan_ts, then keeps the max per scan. That is a many-to-many join that can explode for chatty packages. pd.merge_asof does this in one s...


Submit Your Answer
Markdown supported