Challenge: Trace the Complete Path of a Journey

Given a HashMap with all the points, can you find the starting point and print out the complete path from start to end? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement

In this problem, you have to implement the tracePath() function to find the starting point of a journey and print out the complete path from start to end. An illustration is also provided for your understanding.

Function Prototype:

String tracePath(HashMap<String,String> map);

Here, map is a HashMap containing strings as keys and values corresponding to the cities, where the key is the departure city and the value is the arrival city


It returns the String containing the complete path from the start until the end of the journey.

Sample Input

map = 
    "NewYork" -> "Chicago"
    "Boston" -> "Texas"
    "Missouri" -> "NewYork"
    "Texas" -> "Missouri"
    key -> value

Sample Output

"Boston->Texas, Texas->Missouri, Missouri->NewYork, NewYork->Chicago, "


The starting point of the path is “Boston”, so we find it first to help us traverse the whole path; we get to Texas through Boston, to Missouri through Texas, and so on.

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