PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Check if adding edge creates cycle in digraph

Last updated: Jun 27, 2026

Quick Overview

This question evaluates understanding of graph algorithms, reachability, and algorithmic complexity by determining whether adding a directed edge would introduce a cycle in an otherwise acyclic directed graph.

  • medium
  • Amazon
  • Coding & Algorithms
  • Machine Learning Engineer

Check if adding edge creates cycle in digraph

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You work with a system that stores items and directed relationships between them (for example, item A points to item B). The relationships form a directed graph that is guaranteed to be acyclic (there are no directed cycles, so following pointers will never lead to an infinite loop). Whenever a user creates a new relationship between two items, you add a new directed edge (u_new, v_new). Before committing this change, you must validate that the graph will still contain no directed cycles after adding this edge. Design an algorithm and write code for a function that takes: - an integer n: the number of items labeled from 0 to n - 1, - a list of existing directed edges, where each edge is a pair (u, v) meaning a directed edge from u to v, - a candidate new directed edge (u_new, v_new), and returns a boolean indicating whether adding this new edge keeps the graph acyclic (true) or would introduce at least one directed cycle (false). State the time and space complexity of your approach. Assume n and the number of edges can each be as large as 100000, so your solution should be close to linear in the size of the graph.

Quick Answer: This question evaluates understanding of graph algorithms, reachability, and algorithmic complexity by determining whether adding a directed edge would introduce a cycle in an otherwise acyclic directed graph.

Related Interview 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)
Amazon logo
Amazon
Oct 26, 2025, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
6
0
Practice Read

You work with a system that stores items and directed relationships between them (for example, item A points to item B). The relationships form a directed graph that is guaranteed to be acyclic (there are no directed cycles, so following pointers will never lead to an infinite loop).

Whenever a user creates a new relationship between two items, you add a new directed edge (u_new, v_new). Before committing this change, you must validate that the graph will still contain no directed cycles after adding this edge.

Design an algorithm and write code for a function that takes:

  • an integer n: the number of items labeled from 0 to n - 1,
  • a list of existing directed edges, where each edge is a pair (u, v) meaning a directed edge from u to v,
  • a candidate new directed edge (u_new, v_new),

and returns a boolean indicating whether adding this new edge keeps the graph acyclic (true) or would introduce at least one directed cycle (false).

State the time and space complexity of your approach. Assume n and the number of edges can each be as large as 100000, so your solution should be close to linear in the size of the graph.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Machine Learning Engineer•Amazon Machine Learning Engineer•Amazon Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
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.