FastPrepDetect and Break a Linked-List Cycle
Problems/Detect and Break a Linked-List Cycle

Detect and Break a Linked-List Cycle

Amazon logoAmazon● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon new grad onsite interview. The report asks you to detect a cycle, reason with Floyd's two pointers, find where the cycle starts, and set the last edge of the cycle to null. It does not say how the list is given as input. Our version is about 96% the same as the reported question.

A singly linked list is represented by an integer array next. Node 0 is the head, and next[i] is either the index of node i's successor or -1 when it has no successor. Every node is reachable from the head.

If the list contains a cycle, find its entry node and then find the last node in that cycle, whose successor points back to the entry. Return a copy of next after changing that one offending successor to -1.

If the list has no cycle, return an unchanged copy. Use constant extra space apart from the returned array.

Function

breakLinkedListCycle(next: int[]) → int[]

Examples

Example 1

next = [1,2,3,1]return = [1,2,3,-1]

Nodes 1 -> 2 -> 3 -> 1 form a cycle. Node 3 is the last cycle node, so its successor is replaced with -1.

Example 2

next = [1,2,-1]return = [1,2,-1]

The traversal reaches -1, so there is no cycle and the representation is unchanged.

Example 3

next = [0]return = [-1]

The only node points to itself. It is both the cycle entry and the last node in the cycle, so its successor is removed.

Constraints

  • 1 <= next.length <= 2 * 10^5.
  • -1 <= next[i] < next.length.
  • Every node is reachable by starting at node 0 and repeatedly following successors before a node repeats or -1 is reached.

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Unique Target-Sum Pairs in a Sorted Doubly Linked ListArista Networks · PHONE SCREENArista Networks interview questions
  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Remove Nth Node From End of ListDruva · ONSITE INTERVIEW
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Find the Entry of a Linked List CycleQualys · ONSITE INTERVIEWQualys interview questions

Interview reports that mention this question

  • Amazon New Grad SDE Onsite Interview Questions: 17 Experiences

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
See Amazon hiring insights
Saved
class Solution {
    public int[] breakLinkedListCycle(int[] next) {
      // Write your code here
    }
}
Checking account…