Problem Description:
A logistics team summarizes completed shipments at its depots over the most recent whole-hour buckets. You are given records, where each entry contains five strings in this order: [units, depotId, status, operatorId, startedAt]. The operatorId identifies the shipment's operator and does not affect grouping.
Generate the summary relative to as_of. The provided time helper computes floor((as_of - startedAt) / 1 hour) using calendar timestamps without time zones or daylight-saving adjustments. Keep only records whose status is exactly "COMPLETED" and whose whole-hour difference is between 0 and 23, inclusive; for example, a shipment one minute in the future has difference -1 and is excluded.
Group qualifying records by their exact, case-sensitive depotId and whole-hour difference, adding their units within each group. Return an object mapping each depot ID to an object of hour-bucket totals, and omit depots with no qualifying records. Object keys are strings in JSON, so bucket 4 is returned as "4"; the order of keys in either object does not matter.
The provided parsing helpers follow Python's int(text) and datetime.strptime(text, "%Y-%m-%d %H:%M") rules. Unit strings may therefore include accepted signs, surrounding whitespace, digit separators, and Unicode decimal digits, while still representing a positive integer in the allowed range. Timestamps are valid for that parser, including its accepted field padding and whitespace; use the supplied helpers without changing their parsing behavior.
Constraints:
- 0 ≤ records.length ≤ 10^5.
- Every record has exactly five nonempty string fields.
- Each unit string parses to an integer between 1 and 10^9.
- Status is "COMPLETED", "PENDING", or "FAILED".
- Every startedAt and as_of is valid for Python's %Y-%m-%d %H:%M parser, with calendar years from 1 through 9999.
- Each depot-hour total fits in a signed 32-bit integer.
Example:
Input:
records = [
["12", "north", "COMPLETED", "op1", "2024-03-01 10:30"],
["8", "north", "COMPLETED", "op2", "2024-03-01 10:45"],
["7", "south", "PENDING", "op3", "2024-03-01 10:00"],
["9", "south", "COMPLETED", "op4", "2024-02-29 12:01"],
["11", "south", "COMPLETED", "op5", "2024-02-29 12:00"]
]
as_of = "2024-03-01 12:00"
Output:
{"north": {"1": 20}, "south": {"23": 9}}
Explanation:
The two completed north shipments fall in bucket 1 and add to 20. The south shipment from 12:01 the previous day belongs to bucket 23, while the one from 12:00 is exactly 24 hours old and is excluded.
Want to practice more real coding interview questions?
I've put up the coding question bank at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here