PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Detect cycles in lists and graphs

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

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.

Quick Answer: 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.

Detect Cycle In A Linked List

The linked list is represented by next_indices, where -1 means null. Return whether the list reachable from head contains a cycle.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([1,2,3,1], 0)

Expected Output: True

Explanation: The list cycles back to index 1.

Input: ([1,2,-1], 0)

Expected Output: False

Explanation: The list terminates.

Input: ([0], 0)

Expected Output: True

Explanation: A node that points to itself is a cycle.

Hints

  1. Use Floyd slow and fast pointers.
  2. Treat -1 as the end of the list.

Detect Reachable Cycle In A Directed Graph

Return whether any directed cycle is reachable from the start node.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: {0:[1],1:[2],2:[0]}, 0

Expected Output: True

Explanation: A directed cycle is reachable from start.

Input: ({"a":["b","c"],"b":[],"c":[]}, "a")

Expected Output: False

Explanation: A DAG has no cycle.

Input: ([[1],[2],[]], 0)

Expected Output: False

Explanation: List adjacency is supported.

Hints

  1. Use DFS colors: visiting means on the recursion stack.
  2. Only nodes reachable from start need to be explored.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)