FastPrepReconstruct Flight Departure Chains
Problems/Reconstruct Flight Departure Chains

Reconstruct Flight Departure Chains

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce onsite interview. The report keeps the directed flight legs and one exact rule: each returned chain lists the departures and leaves out its final destination. We also set that the paths share no nodes, and that the routes are listed in alphabetical order. Our version is about 94% the same as the reported question.

You are given directed flight legs in legs, where each pair [departure, arrival] represents one flight.

The legs form one or more vertex-disjoint simple directed paths. Every airport appears at most once as a departure and at most once as an arrival, there are no cycles, and no leg is duplicated.

For each maximal path, return the ordered departure airport of every leg. Do not include the final airport, because it is only an arrival. Order the returned chains lexicographically by their first airport.

Function

reconstructDepartureChains(legs: String[][]) → String[][]

Examples

Example 1

legs = [["a","b"],["b","c"],["c","d"],["x","y"],["y","z"]]return = [["a","b","c"],["x","y"]]

The first path has departures a, b, and c; terminal destination d is omitted. The second path similarly returns x and y.

Example 2

legs = [["B","C"],["X","Y"],["A","B"]]return = [["A","B"],["X"]]

The rows need not arrive in route order. The two roots are sorted as A and X.

Example 3

legs = [["MIA","LAX"]]return = [["MIA"]]

A one-leg path contributes its single departure airport.

Constraints

  • 1 <= legs.length <= 200000.
  • Each row contains exactly two nonempty case-sensitive airport identifiers.
  • Each identifier has length at most 30.
  • The legs form vertex-disjoint simple directed paths with no duplicate edge.

More Salesforce Onsite Interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
  • Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
  • Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
  • Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Add Course Batches All or NothingAmazon · ONSITE INTERVIEW
  • Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Package Install OrderVerily · ONSITE INTERVIEW
  • Validate and Resolve Evaluation ChainsPlaid · PHONE SCREEN · ONSITE INTERVIEWPlaid interview questions
  • Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
  • Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
  • Copy a Doubly Linked List with a Special PointerMicrosoft · ONSITE INTERVIEWMicrosoft interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public String[][] reconstructDepartureChains(String[][] legs) {
      // write your code here
    }
}
Checking account…