A logistics company wants to identify shipments that are at risk of delayed delivery.
The company stores shipment details, route profiles, tracking events, weather alerts, and warehouse-processing logs. Your task is to analyse these records and print shipments whose delay risk level is HIGH or MEDIUM.
The delay risk must be calculated using tracking activity, delay events, route weather impact, warehouse processing time, route-level comparison, and expected delivery time.
Read input from STDIN and print output to STDOUT. Do not print arbitrary strings anywhere in the program, as these contribute to standard output and test cases will fail.
Shipments Table
| Field | Description |
|---|---|
| shipmentId | Unique identifier of the shipment |
| shipmentName | Name or reference of the shipment |
| routeId | Route assigned to the shipment |
| dispatchDay | Day on which the shipment was dispatched |
Every routeId in the Shipments table has exactly one matching entry in the Route Profiles table.
Route Profiles Table
| Field | Description |
|---|---|
| routeId | Unique identifier of the route |
| standardTransitDays |
| Standard number of days required to complete the route |
| maxWarehouseProcessingHours | Maximum allowed total warehouse-processing hours for a shipment on that route |
Tracking Events Table
| Field | Description |
|---|---|
| eventId | Unique identifier of the tracking event |
| shipmentId | Shipment to which the event belongs |
| eventDay | Day on which the event was recorded |
| status | IN_TRANSIT, DELAYED, or DELIVERED |
Weather Alerts Table
| Field | Description |
|---|---|
| alertId | Unique identifier of the weather alert |
| routeId | Route affected by the alert |
| alertDay | Day on which the alert was recorded |
| severity | LOW, MEDIUM, or HIGH |
Warehouse Processing Logs Table
| Field | Description |
|---|---|
| processId | Unique identifier of the processing log |
| shipmentId | Shipment processed at the warehouse |
| processDay | Day on which processing was recorded |
| processingHours | Number of processing hours recorded |
A tracking event is valid only when all conditions are satisfied:
A weather alert is valid only when all conditions are satisfied:
A warehouse processing log is valid only when all conditions are satisfied:
Invalid records must be ignored completely.
Every input record must be processed independently. If identical records appear more than once, each occurrence must be counted separately.
For every shipment, calculate the following values using only valid records.
daysWithoutUpdate = referenceDay - latestTrackingDay. If validTrackingEventCount is 0, daysWithoutUpdate = referenceDay + 1.| Severity | Weight |
|---|---|
| LOW | 1 |
| MEDIUM | 2 |
| HIGH | 3 |
= total delayEventCount of all shipments on that route // number of shipments on that route. All shipments on the route must be considered, including delivered shipments and shipments with no tracking events.delayEventCount > routeAverageDelayEventCount.= dispatchDay + standardTransitDays.Calculate the delay risk score for every shipment:
| Condition | Score Added |
|---|---|
| daysWithoutUpdate >= 5 | 4 |
| validTrackingEventCount == 0 | 3 |
| delayEventCount >= 2 | 3 |
| routeWeatherAlertScore >= 4 | 2 |
| totalWarehouseProcessingHours > maxWarehouseProcessingHours | 3 |
| shipment is above the route delay baseline | 2 |
| referenceDay > expectedDeliveryDay and isDelivered is false | 4 |
riskScore = staleUpdateScore + noTrackingScore + delayEventScore + weatherScore + warehouseScore + routeBaselineScore + overdueScore
| Risk Score | Risk Level |
|---|---|
| 10 or more | HIGH |
| 6 to 9 | MEDIUM |
| Less than 6 | LOW |
A delivered shipment must never be printed, even if its calculated risk score is HIGH or MEDIUM. Only non-delivered shipments with HIGH or MEDIUM risk must be printed.
All IDs, names, route IDs, statuses, and severity values contain no spaces.
referenceDay, the fixed analysis day.Records follow in this order:
shipmentId shipmentName routeId dispatchDayrouteId standardTransitDays maxWarehouseProcessingHourseventId shipmentId eventDay statusalertId routeId alertDay severityprocessId shipmentId processDay processingHoursPrint all eligible shipments in the following format:
shipmentName-riskLevel-riskScore-daysWithoutUpdate
If multiple shipments must be displayed, separate them with a hash symbol (#).
Sort order (apply next condition only when previous is a tie):
Print NA if no shipment qualifies.
100
3
2
4
3
3
S1 Parcel_A R1 10
S2 Parcel_B R1 10
S3 Parcel_C R2 80
R1 20 10
R2 30 8
E1 S1 70 DELAYED
E2 S1 80 DELAYED
E3 S2 99 IN_TRANSIT
E4 S3 90 IN_TRANSIT
W1 R1 30 HIGH
W2 R1 50 MEDIUM
W3 R2 85 LOW
P1 S1 60 15
P2 S2 20 5
P3 S3 85 9
Parcel_A-HIGH-18-20#Parcel_C-MEDIUM-7-10#Parcel_B-MEDIUM-6-1
50
4
2
6
6
5
S1 Alpha R1 10
S2 Beta R1 10
S3 Gamma R2 20
S4 Delta R2 20
R1 20 5
R2 15 8
E1 S1 51 DELAYED
E2 S2 30 DELIVERED
E3 S3 25 UNKNOWN
E4 X9 30 DELAYED
E5 S4 40 DELAYED
E6 S4 41 DELAYED
W1 R1 5 HIGH
W2 R1 20 HIGH
W3 R1 30 MEDIUM
W4 R2 30 HIGH
W5 X9 30 HIGH
W6 R2 51 HIGH
P1 S1 20 6
P2 S2 51 10
P3 S3 30 -1
P4 S4 30 9
P5 X9 25 10
Alpha-HIGH-16-51#Delta-HIGH-16-9#Gamma-HIGH-11-51
Flipkart Grid 8.0 • Pending