posted Feb 15, 2011 3:53 PM by Amin Ariana
[
updated Feb 15, 2011 4:11 PM
]
Tags: Moderate, graphs Problem descriptionYou have a directed connected graph. (for bonus points, drop the "connected" assumption) Write an algorithm that detects a cycle, if any exists, and returns its nodes. Alternatively if you need to simplify the problem, just return a boolean indicating whether you found a cycle.
Grading- Completeness of the C# or pseudo-code algorithm (25%)
- Correctness of the algorithm, demonstrated by quick manual unit tests (25%)
- What is the time and space complexity of your algorithm? (25%)
- Is the most efficient solution, O (n), offered? (25%)
Bonus points:- Did you do it for a possibly disconnected graph?
- Did you return the cycle nodes instead of just a boolean?
- Is your code impressively short and clean?
HintsHint #1 - Try to solve the problem on your own. Read this if you're desperate... Ready? Use of an extra hash table or the addition of dirty bits are both acceptable solutions, as long as you qualify the ugliness of your answer and suggest that a better answer may exist. Hint #2 - If you use this hint without coming up with it yourself, you lose the opportunity to look like a genius in the interview. But here it is if you want to use it anyway: If you and your friend are sitting in a race car each, how can you tell if the very long race track is looped? Hint #3 - Still don't get it? OK. Two words for you: Pointers, and Speed. ReferencesA Senior Developer at Amazon in the Mechanical Turk team asked me this question during a 7-hour interview. |
|