FastPrepDetect a Cycle in a Linked List
Problems/Detect a Cycle in a Linked List

Detect a Cycle in a Linked List

Salesforce logoSalesforce● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce phone screen. The report directly asks whether a linked list has a cycle and names fast and slow pointers. Here the list is given as a finite list of next indexes, so the cycle and each node stay exact. We chose which indexes are valid, that the head can reach the cycle, and the input limits. Our version is about 95% the same as the reported question.

Determine whether a singly linked list contains a cycle reachable from its head. Return true if repeatedly following next links ever visits the same node again; otherwise return false.

The input uses indices to identify nodes. next[i] is the index of node i's successor, or -1 for null. head is the first node index, or -1 for an empty list. Different indices represent different nodes even when their values would match. No node values are needed to detect a cycle.

Only follow nodes that can be reached from head. Ignore all other nodes. Links may point to any valid node index, including the same node. For this exercise, assume the supplied indices are valid and do not change.

Function

hasCycle(next: int[], head: int) → boolean

Examples

Example 1

next = [1,2,1]head = 0return = true

The successor walk revisits node 1, so the reachable list has a cycle.

Example 2

next = [1,2,-1]head = 0return = false

The successor walk reaches null without repeating a node.

Example 3

next = []head = -1return = false

An empty list has no reachable cycle.

Constraints

  • 0 <= next.length <= 100000.
  • Every successor is -1 or an index in next.
  • head is -1 or a valid node index; an empty array has head = -1.

More Salesforce Phone Screen questions

  • Find Rows Common to Two TablesPHONE SCREEN · Seen Oct 2026
  • Merge Two Sorted Linked ListsPHONE SCREEN · Seen Oct 2026
  • Minimum Window of Required WordsPHONE SCREEN · Seen Aug 2026
  • Permutations of Unique CharactersPHONE SCREEN · Seen Aug 2026
  • Minimum Window SubstringPHONE SCREEN · Seen Aug 2026
  • Optimal Account BalancingPHONE SCREEN · Seen Jul 2026
  • Longest Increasing SubsequencePHONE SCREEN · Seen Jul 2026
  • Maximal SquarePHONE SCREEN · Seen Jul 2026
  • ATM Queue Exit OrderPHONE SCREEN · Seen May 2026
  • Good Ways to Split an ArrayPHONE SCREEN · Seen May 2026

Similar questions at other companies

  • Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
  • Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
  • Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
  • Count Nodes in a ChainApple · PHONE SCREENApple interview questions
  • Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
  • Reverse a Linked ListMygate · PHONE SCREENMygate interview questions
  • Linked List Palindrome in Constant SpaceNVIDIA · PHONE SCREENNVIDIA interview questions
  • First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
  • Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Merge Two Sorted Linked ListsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public boolean hasCycle(int[] next, int head) {
        // Write your code here.
    }
}
Checking account…