Detect cycles in lists and graphs
AmazonSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of cycle detection in linked lists and directed graphs, assessing competencies in pointer manipulation, graph traversal strategies, and reasoning about algorithmic time and space complexity within the Coding & Algorithms domain.
Given a singly linked list, determine whether it contains a cycle. Implement an O( 1)-space algorithm and explain the intuition and correctness. Follow-up: Generalize to a directed graph where each node may have multiple children. Given a starting node, determine whether a cycle exists reachable from it. Discuss how you'd handle disconnected graphs, very large graphs that may not fit in memory, and the time/space complexity trade-offs. Provide pseudocode or code for both parts.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.