Problem · Graph

Reconstruct Landmark Journey

MediumCapital OneOA

A traveler visited a series of unique landmarks on a journey. Unfortunately, their travel journal was damaged, and they can no longer remember the exact order of their visits. However, they do have a collection of photos, each showing exactly two landmarks that were visited consecutively. Either landmark could have been visited first.

Given the collection of photos represented as pairs of landmark IDs in travelPhotos, help the traveler reconstruct the complete journey. Each landmark was visited exactly once, and for every consecutive pair of landmarks in the journey, there exists a photo containing both landmarks.

You may reconstruct the journey in either forward or reverse order; both are considered correct.

Examples
01 · Example 1
travelPhotos = [[3, 5], [1, 4], [2, 4], [1, 5]]
return = [3, 5, 1, 4, 2]
  • The photos show that landmarks 3 and 5 were visited consecutively, as were 1 and 4, 2 and 4, and 1 and 5.
  • The journey [3, 5, 1, 4, 2] contains each of those consecutive landmark pairs.
More Capital One problems
drafts saved locally
public int[] solution(int[][] travelPhotos) {
  // write your code here
}
travelPhotos[[3, 5], [1, 4], [2, 4], [1, 5]]
expected[3, 5, 1, 4, 2]
checking account