FastPrepCheck a Linked List for a Cycle
Problems/Check a Linked List for a Cycle

Check a Linked List for a Cycle

Zillow logoZillow● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported at Zillow in a September 2020 review by a candidate for a Big Data software development engineer role in the United States, who rated the experience positive and did not get an offer. The report describes a one-hour technical phone interview run by a developer from the team. The candidate worked on one coding problem in a shared online editor, in a language they picked, and the call ended with a very high-level talk about some system design questions.

The coding problem was to tell whether a linked list contains a cycle.

The report does not say what kind of linked list it was, how the list reaches the code, or how long it can be. Our choices: the list is singly linked; the function hasCycle gets head, its first node, and returns true or false; each test writes a list as its values plus the index the last node links back to, or -1 when the last node links to nothing. The limits (0 to 10,000 nodes, values from -100,000 to 100,000) are ours as well.

Our version is about 90% the same as the reported question.

You are given head, the first node of a singly linked list. Every node stores an integer val and a pointer next to the node after it. Start at head and keep moving to next. If this walk ever arrives at a node it has seen before, it can never stop, and we say there is a cycle. Return true if there is a cycle. Return false if the walk reaches a node whose next is null, or if the list is empty.

Each test writes a list as [values, pos]. values gives the node values in order, starting at head. pos tells where the last node's next leads: the index of that node in values, or -1 when that next is null. Indexes start at 0. The tests use pos to build the links; your function gets only head.

Function

hasCycle(head: CycleListNode) → boolean

Examples

Example 1

head = [[4,9,-2,7,5],1]return = true
head
49-275

The last node, 5 at index 4, links back to 9 at index 1. Starting at head, the walk goes 4, 9, -2, 7, 5 and then arrives at 9 again, so it never stops.

Example 2

head = [[6,1,8],-1]return = false
head
618

Here pos is -1, so the last node 8 has next set to null. The walk visits three nodes and stops.

Example 3

head = [[3],0]return = true
head
3

The single node's next leads back to it, so each step lands on it again.

Example 4

head = [[5,5],-1]return = false
head
55

Both nodes hold 5, but they are two different nodes, and the second one's next is null. A repeated value is not a cycle; the walk must return to a node it has passed.

Constraints

  • 0 <= values.length <= 104
  • -105 <= values[i] <= 105
  • pos is -1 or an index of values, that is 0 <= pos < values.length

More Zillow Phone Screen questions

  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
  • Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • Jump GamePHONE SCREEN · Seen Dec 2022
  • Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
  • Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
  • Employee Free TimePHONE SCREEN · Seen Oct 2022
  • First Unique Character in a StringPHONE SCREEN · Seen Oct 2022

Similar questions at other companies

  • First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
  • Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
  • Merge Point of Two Linked ListsBank of America · OABank of America interview questions
  • Find Where Two Linked Lists MergeExpedia · ONSITE INTERVIEWExpedia interview questions
  • Detect a Linked-List CyclePaytm · ONSITE INTERVIEWPaytm interview questions
  • Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
  • Detect a Linked-List CycleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
  • Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean hasCycle(ListNode head) {
        // Write your code here.
    }
}
Checking account…