Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Detect if a Linked List is Circular in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code detecting a circular linked list?
Consider the following JavaScript code that checks if a linked list is circular. What will it print?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head) {
  if (!head) return false;
  let slow = head;
  let fast = head.next;
  while (fast && fast.next) {
    if (slow === fast) return true;
    slow = slow.next;
    fast = fast.next.next;
  }
  return false;
}

// Create nodes
const a = new Node(1);
const b = new Node(2);
const c = new Node(3);
const d = new Node(4);

// Link nodes
a.next = b;
b.next = c;
c.next = d;
d.next = b; // Circular link here

console.log(isCircular(a));
Afalse
Btrue
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and when they meet.
❓ Predict Output
intermediate
2:00remaining
What is the output of this code for a non-circular linked list?
Look at this JavaScript code that checks if a linked list is circular. What will it print?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head) {
  if (!head) return false;
  let slow = head;
  let fast = head.next;
  while (fast && fast.next) {
    if (slow === fast) return true;
    slow = slow.next;
    fast = fast.next.next;
  }
  return false;
}

// Create nodes
const a = new Node(1);
const b = new Node(2);
const c = new Node(3);
const d = new Node(4);

// Link nodes
a.next = b;
b.next = c;
c.next = d;
d.next = null; // No cycle

console.log(isCircular(a));
Afalse
BReferenceError
Cnull
Dtrue
Attempts:
2 left
💡 Hint
No node points back to an earlier node, so no cycle exists.
🧠 Conceptual
advanced
1:30remaining
Why does the fast pointer start at head.next in cycle detection?
In the cycle detection algorithm, why is the fast pointer initialized to head.next instead of head?
ATo avoid immediate equality with slow pointer at start, preventing false positive
BBecause fast pointer moves slower than slow pointer
CTo skip the first node which is always null
DTo ensure the fast pointer never moves beyond the list length
Attempts:
2 left
💡 Hint
Think about what happens if both pointers start at the same node.
🔧 Debug
advanced
2:00remaining
What error does this code raise when detecting a circular linked list?
This code tries to detect a cycle in a linked list. What error will it raise when run?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head) {
  let slow = head;
  let fast = head.next;
  while (fast && fast.next && fast.next.next) {
    if (slow === fast) return true;
    slow = slow.next;
    fast = fast.next.next;
  }
  return false;
}

const a = new Node(1);
const b = new Node(2);
const c = new Node(3);

a.next = b;
b.next = c;
c.next = a; // cycle

console.log(isCircular(a));
Atrue
Bfalse
CTypeError: Cannot read property 'next' of null
DReferenceError: fast is not defined
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens when fast or fast.next is null.
🚀 Application
expert
3:00remaining
How many nodes are in the circular linked list after these operations?
Given the following code that creates a circular linked list, how many unique nodes does the list contain?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
const fourth = new Node(4);

head.next = second;
second.next = third;
thrid.next = fourth;
fourth.next = second; // cycle starts at second node

// Count unique nodes in the cycle
function countUniqueNodes(head) {
  let slow = head;
  let fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) {
      let count = 1;
      let current = slow.next;
      while (current !== slow) {
        count++;
        current = current.next;
      }
      return count;
    }
  }
  return 0;
}

console.log(countUniqueNodes(head));
A1
B4
C2
D3
Attempts:
2 left
💡 Hint
The cycle starts at the second node, so count nodes from there until it loops back.

Practice

(1/5)
1. What does it mean if a linked list is circular?
easy
A. The linked list has only one node
B. The linked list has no nodes
C. The linked list nodes are sorted in ascending order
D. The last node points back to one of the previous nodes, forming a loop

Solution

  1. Step 1: Understand linked list structure

    A linked list is a chain of nodes where each node points to the next node.
  2. Step 2: Define circular linked list

    If the last node points back to any previous node, it creates a loop, making the list circular.
  3. Final Answer:

    The last node points back to one of the previous nodes, forming a loop -> Option D
  4. Quick Check:

    Circular linked list = last node points back [OK]
Hint: Circular means last node links back to earlier node [OK]
Common Mistakes:
  • Thinking circular means sorted list
  • Confusing empty list with circular list
  • Assuming single node list is always circular
2. Which of the following is the correct way to check if a linked list is circular in JavaScript?
easy
A. Count nodes and compare with array length
B. Use two pointers moving at different speeds and check if they meet
C. Check if head is null only
D. Sort the linked list nodes

Solution

  1. Step 1: Understand Floyd's cycle detection

    Use two pointers: slow moves one step, fast moves two steps. If they meet, list is circular.
  2. Step 2: Eliminate wrong options

    Checking head null only doesn't detect loops. Counting nodes or sorting is unrelated.
  3. Final Answer:

    Use two pointers moving at different speeds and check if they meet -> Option B
  4. Quick Check:

    Two-pointer cycle detection = correct method [OK]
Hint: Two pointers meeting means circular list [OK]
Common Mistakes:
  • Only checking if head is null
  • Trying to sort linked list to detect cycle
  • Counting nodes without loop detection
3. What will be the output of the following code snippet?
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.next = third;
third.next = second; // creates a loop

function isCircular(head) {
  let slow = head;
  let fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) return true;
  }
  return false;
}

console.log(isCircular(head));
medium
A. false
B. undefined
C. true
D. TypeError

Solution

  1. Step 1: Analyze linked list structure

    Nodes 1 -> 2 -> 3, and third node points back to second, creating a loop.
  2. Step 2: Trace two-pointer loop detection

    Slow and fast pointers will eventually meet inside the loop, so function returns true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Loop detected = true [OK]
Hint: Loop causes pointers to meet, returning true [OK]
Common Mistakes:
  • Assuming no loop because list looks linear
  • Confusing pointer equality check
  • Expecting false without tracing pointers
4. Find the error in this code that tries to detect a circular linked list:
function isCircular(head) {
  let slow = head;
  let fast = head;
  while (fast.next && fast.next.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow == fast) return true;
  }
  return false;
}
medium
A. The while condition should check if fast and fast.next exist
B. The comparison should use === instead of ==
C. The slow pointer should move two steps instead of one
D. The function should return false inside the loop

Solution

  1. Step 1: Check while loop condition safety

    fast.next.next is accessed without confirming fast is not null, can cause error if fast is null.
  2. Step 2: Correct condition to avoid runtime error

    Condition should be while (fast && fast.next) to safely access fast.next.next.
  3. Final Answer:

    The while condition should check if fast and fast.next exist -> Option A
  4. Quick Check:

    Check fast and fast.next before accessing next nodes [OK]
Hint: Always check fast and fast.next before fast.next.next [OK]
Common Mistakes:
  • Not checking if fast is null before accessing fast.next.next
  • Using == instead of === (not critical here)
  • Moving slow pointer two steps incorrectly
5. Given a linked list where the last node points back to the head, how can you modify the detection function to also return the node where the loop starts?
function detectLoopStart(head) {
  let slow = head;
  let fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) {
      // Loop detected
      slow = head;
      while (slow !== fast) {
        slow = slow.next;
        fast = fast.next;
      }
      return slow;
    }
  }
  return null;
}
hard
A. The function returns the node where the loop starts by resetting slow to head and moving both pointers one step
B. The function returns true if a loop exists but does not find the start node
C. The function incorrectly moves fast pointer one step after detection
D. The function will always return null because slow and fast never meet

Solution

  1. Step 1: Detect loop using two pointers

    Slow and fast pointers meet inside the loop confirming a cycle.
  2. Step 2: Find loop start node

    Reset slow to head, move slow and fast one step at a time until they meet again; this node is loop start.
  3. Final Answer:

    The function returns the node where the loop starts by resetting slow to head and moving both pointers one step -> Option A
  4. Quick Check:

    Reset slow and move both pointers to find loop start [OK]
Hint: Reset slow to head after detection to find loop start [OK]
Common Mistakes:
  • Returning true instead of loop start node
  • Moving fast pointer incorrectly after detection
  • Assuming slow and fast never meet
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard