Packages still moving

ranking

Packages still moving

Amazon SQL Interview Question

Every hour, an Amazon fulfillment center needs the packages that are somewhere in the building and not yet out of it, so a supervisor can walk the floor and find them. A package is still in play when its most recent scan is anything other than the delivery scan, and the stage it stopped at says where to look.

Return the package_id, the facility holding it, the last stage it reached as last_stage, and when that happened as last_scanned_at.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

amazon_fulfillment_scansTable24 rows

Column NameType
scan_idBIGINT
package_idVARCHAR
facilityVARCHAR
scanned_atTIMESTAMP
stageVARCHAR

amazon_fulfillment_scansExample Input

scan_idpackage_idfacilityscanned_atstage
101P001BLR12024-11-01 09:05:00received
102P001BLR12024-11-01 10:20:00picked
103P001BLR12024-11-01 11:15:00packed
104P001BLR12024-11-01 13:40:00shipped
105P001BLR12024-11-02 09:30:00delivered
123P006HYD32024-11-01 07:50:00received
124P006HYD32024-11-01 09:40:00picked

Example Output

package_idfacilitylast_stagelast_scanned_at
P006HYD3picked2024-11-01 09:40:00

Explanation

The most recent scan on P006 is the pick, so it is still somewhere inside HYD3 and the supervisor needs to find it. P001 was last scanned as delivered, so it has left the building and is not listed.

The example above is a small slice of the data. Your query runs against the full tables.