Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
The question was the following. I'm rephrasing the question to make it clear for everyone to understand: - You are going on a one-way flight trip that includes billions of layovers. - You have 1 ticket for each part of your trip (i.e: if your trip is from city A to city C with a layover in city B, then you will have 1 flight ticket from city A to city B, and 1 flight ticket from city B to city C. - Each layover is unique. You are not stopping twice in the same city. - You forgot the original departure city. - You forgot the final destination city. - All the tickets you have are randomly sorted. Question are: - Design an algorithm to reconstruct your trip with minimum complexity. - How would you improve your algorithm. Example: - randomly sorted: New York->London San Francisco-> Hong Kong Paris->New York London->San Francisco - sorted: Paris->New York New York->London London->San Francisco San Francisco-> Hong Kong
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (8)
topological sorted, Time = O(N)
Helpful Answer?
Yes |
No
Inappropriate?
Start from node without any entering edge O(n) and follow the path O(n).
Helpful Answer?
Yes |
No
Inappropriate?
Note that this solution will only work for unique layovers...
The starting cities are the keys, and the ending cities are the values
Helpful Answer?
Yes |
No
Inappropriate?
loop through all the entries {
add the start->end pairs in there (checking to see if the start doesn't already exist) and an empty entry for the end city (again, only if it doesn't already exist). In the case that you add a start->end entry, the start becomes your global start.
}
Then just follow the white rabbit from the global start at the end to get your list :)
If I've screwed up somewhere please let me know, thanks!
Helpful Answer?
Yes |
No
Inappropriate?
Then, follow the path, which I think is O(n^2) because you have to check the remaining remaining tickets each time.
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by neakor:
1. Select a ticket from the pool of tickets that is different from all the previously selected starting tickets.
2. Keep a count on how many tickets you have used.
3. Keep walking on the ticket path until you exhaust all possible paths without going back. Increment count every time a ticket is used.
4. If the final count equals the total number of tickets you have, you've got the path sorted.
5. Otherwise, repeat from step 1.
Can be improved via parallelization where you can shoot off multiple threads each having its own starting ticket in parallel. Whenever a thread completes the sorting based on step 4, stop all other threads.