The Destination City coding problem gives you a list of paths where each path is a pair [cityA, cityB], representing a one-way trip from A to B. You are told that the paths form a line without any cycles. Your task is to find the "destination city," which is the city that has no outgoing path to any other city.
Yelp and Meta use this "Easy" question to test your understanding of graph properties and your ability to use sets for membership testing. It's a test of "Out-degree" logic. It evaluations whether you can identify the node in a directed graph that has an out-degree of zero.
The Hash Table interview pattern (specifically a Hash Set) is the most efficient approach here.
Set of all cities that appear as a starting point (cityA).cityB values.cityB that is not in the set of starting points is the destination city.Paths: [["London", "NY"], ["NY", "Lima"], ["Lima", "Sao Paulo"]].
{"London", "NY", "Lima"}.Whenever a problem asks you to find an element that appears in one list but not another, think of a Hash Set. It reduces the lookup time from to .