The problem sounds like a simple loop until you realize the timestamps aren't sorted and might have timezone offsets.
Start by clarifying requirements and edge cases, then outline a solution that groups events by order ID, sorts them by timestamp, and validates the sequence of events. Compute active time per order by pairing ACCEPTED with DELIVERED or CANCELED, rounding down to whole minutes, and sum across orders. Discuss trade-offs between sorting all events globally versus per order, and how to handle malformed sequences.
Pro tip: Explicitly state your assumptions about malformed sequences (e.g., missing ACCEPTED, duplicate events, out-of-order timestamps) and propose a deterministic policy, such as ignoring incomplete orders or logging errors. This shows you think about robustness and production readiness.
Ask about timezone handling, rounding rules, and what constitutes a malformed sequence. Confirm whether to ignore or error on malformed orders, and whether multiple ACCEPTED events per order are possible.
Use a hash map to group events by order ID, then sort each order's events by timestamp. Parse timestamps into a comparable format (e.g., epoch milliseconds) handling timezone offsets.
For each order, check that events follow the expected order: CREATED (optional), ACCEPTED, then PICKED_UP (optional), then DELIVERED or CANCELED. Flag orders with missing or out-of-order events as malformed.
For valid orders, find the ACCEPTED timestamp and the subsequent DELIVERED or CANCELED timestamp. Compute the difference in minutes, round down to whole minutes, and accumulate total pay.
Decide on a policy for malformed orders (e.g., skip, count as zero, or throw error) and explain it. Return total pay and optionally report malformed orders for debugging.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.