Sobes.tech
Junior — Senior

Finding a path between airports using depth-first search

livecode

Task condition

It is necessary to implement a function that determines any possible sequence of flights from the departure airport to the destination. The function takes three arguments: the departure airport code, the destination airport code, and a ticket provider function. The result of the function should be a Promise that either resolves to an array of all intermediate points (e.g., ['A','B','C']) or a string "no way" if the path is unavailable.

It is guaranteed that for the given points, there is either a single unique route or none at all (no situations with multiple alternative branches). When searching, it is forbidden to run multiple requests simultaneously, and the use of async/await syntax and generators is prohibited.

const flights = [
  ['A', 'B'],
  ['A', 'C'],
  ['A', 'D'],
  ['A', 'O'],
  ['D', 'K'],
  ['D', 'L'],
  ['D', 'M'],
  ['M', 'Q'],
  ['M', 'Z'],
  ['O', 'P'],
  ['L', 'G'],
  ['L', 'F'],
  ['F', 'Y'],
];

findPath('A', 'N', fetchFlighting).then(console.log) // Promise.resolve(['A', 'D', 'N'])
findPath('A', 'Q', fetchFlighting).then(console.log) // Promise.resolve(["A", "D", "M", "Q"])
findPath('A', 'W', fetchFlighting).then(console.log) // 'no way'