Rebuild a Container Itinerary From Unordered Leg Records

Last updated: November 29, 2026

Quick Overview

You receive a shipment's transport legs as unordered (origin, destination) port pairs from several carriers. Return the full ordered port sequence, or report an error if the legs do not form one continuous chain.

Flexport
Coding & Algorithms
Software Engineer
Flexport
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


You receive a shipment's transport legs as unordered (origin, destination) port pairs from several carriers. Return the full ordered port sequence, or report an error if the legs do not form one continuous chain.

Flexport stitches multi-leg journeys (feeder vessel, mainline vessel, rail, truck) from data that arrives in no particular order. This style of question checks basic hash map modeling and, more importantly, whether you validate messy partner data instead of assuming a clean input.

What the Interviewer Expects
  • Model legs as a successor map and find the unique start port
  • Detect branches (a port with two outgoing or incoming legs)
  • Detect disconnected pieces and cycles
  • Run in linear time
  • Explain what error message an operations user would see
Key Topics to Cover
Hash Map
Graph Traversal
Data Validation
Linked Chains
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
  • What if a container legitimately revisits a port, for example a return to origin?
  • How would you merge two partial itineraries reported by different carriers for the same container?
  • How would you handle a missing leg so the ops team can see where the gap is?
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: list of (origin, destination) UN/LOCODE strings. Output: ordered list of ports from first origin to final destination. Assumption to confirm: no port is visited twice, so every port has at mo...

High-Level Design

Build next[origin] = destination and a set of ports with an incoming leg. The start is the one origin with no incoming leg; zero starts means a cycle, more than one means disconnected pieces. Walk `...


Submit Your Answer
Markdown supported

Related Questions