Problem · Database

Longest Cumulative Kitchen Delivery Time

MediumWolverine Trading logoWolverine TradingINTERNNEW GRADOA

Problem statement

The table delivery_access_log contains one day of keycard events for companies making deliveries to a kitchen.

Each row records a company_id, an event_timestamp_ns measured in nanoseconds since midnight, and an event_type equal to ENTER or EXIT.

For each company, calculate the total duration of all completed visits. Select the company with the greatest exact cumulative duration. Convert only that winning duration to minutes, round it down to an integer, and return exactly one row with these columns in order:

  • company_id
  • duration_in_kitchen_minutes

If multiple companies have the same exact cumulative duration, return the one with the smaller company_id.

Table schema

MySQLPostgreSQLPandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

delivery_access_log

One day of kitchen access events for delivery companies.

ColumnTypeNullableDescription
company_idPKIntegerNoDelivery-company identifier.
event_timestamp_nsPKIntegerNoNanoseconds elapsed since midnight.
event_typeTextNoENTER or EXIT.

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
company_idIntegerNo
duration_in_kitchen_minutesIntegerNo

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • event_type is exactly ENTER or EXIT.
  • event_timestamp_ns is an integer from 0 through 86399999999999.
  • Rows may appear in any physical order; events are ordered separately for each company by event_timestamp_ns.
  • For this exercise, assume every company has at least one complete, non-overlapping visit and its events alternate ENTER then EXIT when ordered by event_timestamp_ns.
  • For this exercise, assume event_timestamp_ns values are distinct within each company.
  • For this exercise, assume companies are ranked by their exact cumulative nanoseconds, and flooring is applied only to the winning company's returned minute value.
  • For this exercise, assume equal exact cumulative durations are resolved by returning the smaller company_id.

More Wolverine Trading problems