Cheapest Cainiao Parcel Route Under a Delivery Deadline

Last updated: November 29, 2026

Quick Overview

Cainiao hubs are nodes and directed transport legs have a travel time in whole hours (at least 1) and a cost. Find the minimum cost to move a parcel from hub `src` to hub `dst` arriving within `deadline` hours, or -1 if impossible.

Alibaba
Coding & Algorithms
Software Engineer
Alibaba
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Cainiao hubs are nodes and directed transport legs have a travel time in whole hours (at least 1) and a cost. Find the minimum cost to move a parcel from hub `src` to hub `dst` arriving within `deadline` hours, or -1 if impossible.

A harder onsite problem in the style of Alibaba's logistics engineering (Cainiao), where routing must balance cost against promised delivery times for Tmall orders. The interviewer checks that you see why plain Dijkstra on cost fails with a second constraint and that you can design a correct state-space DP with clear complexity.

What the Interviewer Expects
  • Explain why Dijkstra on cost alone fails: the cheapest path may miss the deadline
  • Expand the state to (hub, elapsed hours)
  • Exploit integer, positive travel times to process states in increasing time order
  • Implement the DP correctly and return -1 when unreachable
  • Give O(T * (V + E)) time and discuss when that is acceptable
  • Mention dominance pruning or Dijkstra over (cost, time) labels as an alternative
Key Topics to Cover
constrained shortest path
dynamic programming over states
Dijkstra limitations
graph modeling
logistics routing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the actual route, not just its cost?
  • Travel times are now real numbers and the deadline is large. What would you do instead of a time-indexed table?
  • Each hub also has a sorting delay that depends on arrival hour (shifts). How does the state change?
  • How would you answer many queries with different deadlines from the same source efficiently?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: n hubs, legs as (u, v, hours, cost) with hours >= 1 integers and non-negative costs, src, dst, integer deadline. Output: minimum total cost of a path with total hours `<= deadline...

High-Level Design

Plain Dijkstra on cost settles each hub once, at its cheapest cost, but that cheapest path may be slow; a pricier but faster prefix can be the only feasible one. Example: 0->1->3 costs 20 over 4 hou...


Submit Your Answer
Markdown supported