Challenge: Trace the Complete Path of a Journey

Test your knowledge on hash table traversal with this coding exercise!

Problem Statement #

You have to implement the tracePath() function which will take in a list of source-destination pairs and return the correct sequence of the whole journey from the first city to the last.

Input #

A map, represented as a JavaScript object, containing string pairs of source-destination cities just like hash tables do.

Output #

A list of source-destination pairs in the correct order.

Sample Input #

map = {
  "NewYork": "Chicago",
  "Boston": "Texas",
  "Missouri": "NewYork",
  "Texas": "Missouri"
}
key: value

Sample Output #

[["Boston", "Texas"] , ["Texas", "Missouri"] , ["Missouri", "NewYork"] , ["NewYork", "Chicago"]]

Note that, our implementation of Hash Table currently works only for integer keys. You can use JavaScript’s built-in Hashmap, which works for all types of keys. Look at the following code snippet to see how you can use JavaScript’s Hashmap.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.