Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

FedEx Smart Center: Parcel Grouping & Vehicle Routing

1. Project Overview

Role: Software Engineering Intern Organization: FedEx Smart Center, IIT Madras Collaboration Duration: Sep 2024 – Nov 2024

The Smart Center team was building a reusable e-commerce logistics framework that could be adopted by new e-commerce platforms to build their delivery infrastructure.

My primary task was the parcel grouping and vehicle allocation problem.

Given:

  • A warehouse
  • A set of delivery locations
  • Number of parcels at each location
  • Parcel weights/capacity requirements
  • A limited number of delivery vehicles
  • Vehicle capacity constraints

the objective was to group delivery locations and allocate vehicles while keeping the resulting delivery movement efficient.

The project also involved a quick-commerce use case, where a user's request had to be mapped to the nearest available store.


2. High-Level Architecture

                         E-commerce Requests
                                |
                                v
                         Warehouse / Store
                                |
                 +--------------+--------------+
                 |                             |
                 v                             v
        Parcel Grouping                 Quick Commerce
                 |                             |
                 v                             v
        Delivery Locations              User Location
                 |                             |
                 v                             v
        Distance Calculation             KD-Tree
                 |                             |
                 v                             v
             Graph                     Nearest Store
                 |
                 v
               MST
                 |
                 v
          Rooted Delivery Tree
                 |
                 v
                DFS
                 |
          +------+------+
          |             |
          v             v
     Parcel/Weight   Vehicle Capacity
     Aggregation       Allocation
          |             |
          +------+------+
                 |
                 v
          Delivery Groups

3. Parcel Grouping Problem

For a warehouse (W_i), there are (k_i) delivery locations.

Each location has:

Location
├── latitude
├── longitude
├── parcel count
└── weight / capacity requirement

The fleet has a limited number of vehicles, each with a capacity constraint.

A simple calculation such as:

total parcel weight / vehicle capacity

is insufficient because it ignores where the parcels need to be delivered.

For example:

Warehouse
   |
   +---- A
   |
   +---- B
   |
   +---- C

Even if the total weight fits into two vehicles, assigning geographically distant delivery locations to the same vehicle may result in inefficient routes.

Therefore, the grouping process considers both:

  • Geographic relationship between delivery locations
  • Vehicle capacity

4. Geographic Distance

Delivery locations are represented using latitude and longitude.

For distance calculations, the project used the Haversine formula to calculate geographic distance between two coordinates.

For two locations:

A = (lat1, lon1)
B = (lat2, lon2)

the Haversine distance gives the approximate great-circle distance between them.

This distance was used as the edge weight when constructing the delivery graph.

Important Questions

  • Why Haversine instead of Euclidean distance?
  • Why not use actual road-network distance?
  • What happens when two locations have a small Haversine distance but a large road distance?
  • What is the computational cost of calculating all pairwise distances?
  • Can distance calculations be cached?
  • How does the approach behave for very large numbers of delivery locations?

5. Fully Connected Delivery Graph

For a warehouse with (k_i) delivery locations, a fully connected graph was constructed.

Each node represents:

Warehouse
or
Delivery Location

Each edge represents the geographic distance between two nodes.

Example:

             A
           / | \
          /  |  \
         /   |   \
        B----W----C
         \   |   /
          \  |  /
           \ | /
             D

For (k_i) nodes, the fully connected graph contains:

$$ E = \frac{k_i(k_i-1)}{2} $$

edges.

Therefore:

Edges = O(k_i²)

This is an important scalability consideration.

Important Questions

  • Why construct a fully connected graph?
  • Why not construct a sparse graph?
  • What happens when (k_i) becomes very large?
  • How much memory does the graph require?
  • Could you avoid explicitly storing every edge?
  • Could spatial indexing reduce the number of candidate edges?
  • What was the maximum number of delivery locations handled?

6. Minimum Spanning Tree

The fully connected graph was reduced into a Minimum Spanning Tree (MST).

The resulting tree captures the lower-cost geographic connections between the delivery locations.

Conceptually:

Fully Connected Graph

       A ----- B
      /|\     /|
     / | \   / |
    C--W--D--E
     \ |  \ /
      \|   /
       F

             |
             v

          MST

             W
           /   \
          A     B
         / \     \
        C   D     E
                 /
                F

The tree structure was then used as the basis for the subsequent aggregation and vehicle allocation logic.

⚠️ Important

An MST minimizes the sum of selected tree-edge weights.

It does not automatically solve the Vehicle Routing Problem and does not inherently minimize the actual distance travelled by delivery vehicles.

The MST should therefore be understood as a tree-based representation/heuristic for the geographic delivery structure, followed by additional grouping and vehicle-allocation logic.

Critical Interview Questions

  • Why MST for a vehicle-routing problem?
  • What exactly does MST optimize?
  • Does minimum MST weight imply minimum delivery distance?
  • Why not TSP?
  • Why not Vehicle Routing Problem algorithms?
  • Why not a savings algorithm?
  • Why not formulate this as an optimization problem?
  • What assumptions make an MST useful here?
  • What are the limitations of this approach?
  • Can two different MSTs exist?
  • If multiple MSTs have the same total weight, which one is selected?
  • Does the MST guarantee that the warehouse has a useful position in the tree?
  • What happens if the warehouse becomes a leaf?

This is one of the highest-risk areas of the project and should be explained only according to the actual implementation.


7. Rooting the Delivery Tree

After constructing the tree, the warehouse was treated as the starting point for delivery traversal.

Example:

              Warehouse
              /       \
             A         B
           /   \       |
          C     D      E

The warehouse acts as the source from which delivery requirements are aggregated.

The tree itself is an undirected MST. Treating the warehouse as the root is a subsequent representation of that tree for the DFS processing.

Important Questions

  • Does rooting the MST change the MST?
  • Why should the warehouse be the root?
  • Does rooting the tree guarantee an optimal delivery route?
  • What happens if the warehouse is a leaf?
  • Why not construct a tree specifically optimized around the warehouse?

8. DFS-Based Aggregation

Once the delivery tree is available, a DFS is performed from the warehouse.

The DFS processes child subtrees and propagates their requirements toward the warehouse.

For example:

              W
            /   \
           A     B
         /  \     \
        C    D     E

Processing occurs approximately as:

C
D
A
E
B
W

At each node, information from its children is combined.

The information includes:

  • Parcel requirements
  • Weight/capacity requirements
  • Vehicles required
  • Partially filled vehicle capacity

This allows the algorithm to reason about vehicle requirements at the subtree level instead of only at the warehouse.


9. Why Warehouse-Level Capacity Division Is Not Enough

Consider:

Vehicle capacity = 100 kg

A = 100 kg
B = 100 kg
C = 100 kg

Total demand:

300 kg

A simple calculation gives:

ceil(300 / 100) = 3 vehicles

But this tells us nothing about how those vehicles should be assigned geographically.

The DFS instead processes individual branches and maintains capacity information while combining subtrees.

This preserves the relationship between:

Geographic subtree
        +
Parcel demand
        +
Vehicle capacity

10. Vehicle Capacity Tracking

At each node, the algorithm maintains a representation of the vehicles serving that subtree.

Conceptually:

Vehicle Capacity = 100

Node A:

Vehicle 1 = 100
Vehicle 2 = 100
Vehicle 3 = 60

The third vehicle has remaining capacity:

100 - 60 = 40

When another subtree is combined, its demand can potentially use the available capacity before another vehicle is created.

Example:

Existing:

[100, 100, 60]

New demand:

30

The result can become:

[100, 100, 90]

instead of immediately creating another vehicle.

If the new demand exceeds the available capacity:

Existing:

[100, 100, 60]

New demand:

70

then:

30 → existing vehicle
40 → new vehicle

giving:

[100, 100, 90, 40]

The exact packing behaviour depends on the implementation and should be reconstructed from the source code before claiming a particular packing strategy.


11. Distance-Based Grouping / Merging

After building the geographic tree and performing aggregation, a further heuristic can be applied to avoid unnecessarily separating geographically close deliveries.

The basic idea discussed was:

If distance(A → W) < threshold:
    A can potentially be merged with the warehouse-side group

Similarly, nearby branches can be considered for merging.

Example:

             W
            / \
           A   B

If:

distance(A, W) < threshold

then A may be considered sufficiently close to W to merge its delivery grouping.

The threshold controls how aggressively nearby locations are grouped.


12. Merging Heuristic: Risks

This is another area that needs careful explanation.

A distance threshold is a heuristic, not a proof of global optimality.

A locally good merge may produce a globally worse route.

For example:

             W
            / \
           A   B

A is close to W
B is also close to W

but

A -------- B

are far apart

Whether merging them is actually beneficial depends on:

  • Vehicle capacity
  • Delivery sequence
  • Actual road distance
  • Route structure
  • Other deliveries
  • Number of vehicles

Questions

  • Why use a threshold?
  • How was the threshold selected?
  • Is it fixed?
  • Is it learned from data?
  • Does the threshold depend on vehicle capacity?
  • Can merging increase total distance?
  • Can merging violate vehicle capacity?
  • Does merge order matter?
  • Can greedy merging produce a poor global solution?
  • What happens when three branches are all individually close to the warehouse?
  • Why use warehouse distance rather than complete route cost?
  • Could the heuristic be improved?

13. Haversine Distance vs Actual Road Distance

The project uses geographic distance rather than actual driving distance.

Example:

A -------- B
    river

The Haversine distance could be small:

2 km

while the actual driving route might be:

15 km

because the road network requires a detour.

Therefore:

Haversine distance is a geographic approximation, not an exact estimate of fuel consumption or driving distance.

Questions

  • Why was Haversine sufficient for the grouping stage?
  • Why not use Google Maps / OSRM / another routing service?
  • What would change if actual road distances were used?
  • How expensive would pairwise road-distance queries become?
  • Could road distances be cached?
  • Could a road graph be precomputed?

14. Quick-Commerce: Nearest Store

A separate use case involved quick commerce.

Here, the problem was:

Given a user's location, find the nearest store.

Unlike the dynamic-cadet problem in Udaan, the store locations were comparatively stable and limited in number.

A KD-tree was therefore suitable for spatial nearest-neighbour queries.

                  User
                   |
                   v
             (latitude, longitude)
                   |
                   v
              KD-tree
                   |
                   v
             Nearest Store

A naïve implementation would calculate the distance to every store:

User → Store 1
User → Store 2
User → Store 3
...
User → Store N

which requires examining all stores.

A KD-tree organizes the spatial points to make nearest-neighbour queries more efficient in typical cases.


15. Why KD-Tree for FedEx but Not Udaan?

This distinction is important.

FedEx

Stores
  ↓
Relatively static
  ↓
Build KD-tree
  ↓
Nearest-neighbour queries

Udaan

Cadets
  ↓
Locations change
  ↓
Availability changes
  ↓
Frequent updates
  ↓
Global spatial index becomes harder to maintain

Udaan also did not require an exact nearest-cadet query during initial notification.

It was sufficient to identify geographically nearby candidates and notify them.

This made the pincode-based expansion approach more appropriate for that particular design.

Questions

  • Why not use a KD-tree in Udaan?
  • What if cadets move frequently?
  • Could a dynamic KD-tree be used?
  • What is the update complexity?
  • Why not maintain one KD-tree per pincode?
  • What happens if there are millions of cadets?
  • Why not use an R-tree?
  • Why not use a spatial database?
  • When would a KD-tree actually become preferable again?

16. Complexity

For (k) delivery locations:

Complete graph

Vertices = O(k)
Edges    = O(k²)

DFS

After the tree has been constructed:

O(V + E)

For a tree:

E = V - 1

so DFS is:

O(k)

The expensive part is therefore primarily:

Distance generation
        +
Complete graph construction
        +
MST computation

rather than the DFS itself.

Questions

  • What was the largest (k)?
  • Why was (O(k^2)) acceptable?
  • How would you scale this to 100,000 locations?
  • Can the complete graph be avoided?
  • Could a spatial data structure generate only nearby edges?
  • Could the work be parallelized?
  • Could different warehouses be processed independently?

17. Important Edge Cases

One delivery location

W → A

No meaningful grouping is necessary.

Delivery demand exceeds vehicle capacity

Vehicle capacity = 100
A = 250

The algorithm needs multiple vehicles or another handling mechanism.

Very distant delivery clusters

       A B C

       W

       X Y Z

Combining both clusters may produce inefficient routes.

Very dense delivery cluster

      A B C
     D E F
      G H

Grouping becomes more attractive, subject to vehicle capacity.

Long chain

W -- A -- B -- C -- D

The MST/tree structure may produce very different traversal characteristics from a star-shaped topology.

Multiple valid MSTs

Two or more MSTs can have the same total weight.

The downstream DFS and grouping behaviour may differ depending on which tree is selected.

Questions

  • What happens if multiple MSTs exist?
  • Does your result depend on the chosen MST?
  • What happens when a delivery location has zero parcels?
  • What happens when a parcel exceeds vehicle capacity?
  • What happens when there are more required vehicles than available vehicles?
  • What happens when locations have identical coordinates?

18. System Design Questions

An interviewer can move from the algorithm to the backend system.

Scaling

Suppose FedEx has 10,000 warehouses. How would you process them?

Potential architecture:

                  Routing Requests
                        |
                        v
                    Job Queue
                        |
          +-------------+-------------+
          |             |             |
          v             v             v
       Worker 1      Worker 2      Worker 3
       Warehouse A  Warehouse B   Warehouse C
          |             |             |
          +-------------+-------------+
                        |
                        v
                  Route Results

Questions

  • How would you partition the workload?
  • Why partition by warehouse?
  • Can two warehouses be processed independently?
  • How would you retry failed jobs?
  • How do you prevent duplicate processing?
  • How would you make the job idempotent?
  • What happens if a worker crashes halfway through processing?
  • What if parcel data changes while routing is running?
  • How would you maintain a consistent snapshot?
  • How would you monitor routing jobs?
  • What metrics would you collect?

19. Backend / Infrastructure Questions

Since this project is being discussed for an infrastructure/backend interview, expect questions such as:

API

  • What API would you expose for creating a routing request?
  • Is routing synchronous or asynchronous?
  • Why?
  • What does the request payload look like?
  • What does the response look like?
  • How would you handle a request that takes several minutes?

Concurrency

  • What happens if two routing jobs are started for the same warehouse?
  • How do you prevent conflicting assignments?
  • What if vehicle availability changes during computation?

Reliability

  • What happens if the routing worker crashes?
  • What happens if the database becomes unavailable?
  • How do you retry?
  • How do you prevent duplicate vehicle assignments?

Scalability

  • How would you handle 10× traffic?
  • What part of the system becomes the bottleneck first?
  • How would you profile it?
  • How would you horizontally scale it?

20. Most Dangerous Interview Questions

These are the questions I would prepare first before discussing the project in depth:

1. Why did you use an MST for a vehicle-routing problem?

2. What exactly does your MST optimize?

3. Does minimizing MST weight minimize fuel consumption?

4. Why isn't this simply a TSP / VRP problem?

5. Why did you construct a fully connected graph?

6. What happens when the number of delivery locations becomes huge?

7. How did you choose the warehouse as the root?

8. Does rooting the MST make the warehouse optimal?

9. Explain your DFS with a concrete numerical example.

10. Why can't you simply calculate:
       ceil(total_weight / vehicle_capacity)?

11. Exactly how did you maintain partially filled vehicles?

12. What exactly does your merging heuristic do?

13. How did you select the merging threshold?

14. Can your greedy merging produce a suboptimal route?

15. Why Haversine instead of road distance?

16. How would you improve this algorithm today?

17. What was the actual time and space complexity?

18. What was the largest dataset you tested?

19. What happens if there are multiple valid MSTs?

20. What happens if vehicle capacity is insufficient?

21. How would you distribute this computation across thousands
    of warehouses?

22. How would you make the routing job fault tolerant?

23. How would you handle changes to parcel data while routing?

24. Why was KD-tree appropriate for the quick-commerce problem?

25. Why wasn't KD-tree appropriate for Udaan?

21. What You Should Be Able to Draw on a Whiteboard

Before the interview, you should be able to reproduce this without looking at the README:

                 Warehouse
                    |
          +---------+---------+
          |                   |
       Subtree A           Subtree B
       /      \               |
      A1      A2              B1
       |       |               |
    parcels parcels         parcels
       \       /               |
        \     /                |
         DFS aggregation
              |
              v
       Vehicle capacity
          aggregation
              |
              v
       Vehicle assignment

And separately:

               User
                 |
                 v
          Store coordinates
                 |
                 v
              KD-tree
                 |
                 v
          Nearest Store

The MST and merging sections should be treated as the two areas requiring the most preparation, because those are where an interviewer can challenge the correctness of the algorithm rather than simply asking implementation questions.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors