Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Detect if a Linked List is Circular in DSA Typescript - 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 circular linked list detection code?
Consider the following TypeScript code that checks if a linked list is circular. What will it print?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head: Node | null): boolean {
  if (!head) return false;
  let slow: Node | null = head;
  let fast: Node | null = 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; // Creates a cycle

console.log(isCircular(a));
Atrue
Bfalse
CTypeError
Dundefined
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and when they meet in a cycle.
❓ Predict Output
intermediate
2:00remaining
What is the output of this code with a non-circular linked list?
Given the following TypeScript code, what will be printed?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head: Node | null): boolean {
  if (!head) return false;
  let slow: Node | null = head;
  let fast: Node | null = 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
Bnull
CReferenceError
Dtrue
Attempts:
2 left
💡 Hint
No node points back to an earlier node, so no cycle exists.
🔧 Debug
advanced
2:00remaining
What error does this code raise when checking for circular linked list?
Examine this TypeScript code snippet. What error will it raise when run?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

function isCircular(head: Node | null): boolean {
  if (!head) return false;
  let slow: Node | null = head;
  let fast: Node | null = head.next;
  while (fast && fast.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 = null;

console.log(isCircular(a));
ATypeError: Cannot read property 'next' of null
BSyntaxError
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Check how the code handles the end of the list when fast or fast.next is null.
🧠 Conceptual
advanced
1:30remaining
Which statement about detecting circular linked lists is true?
Choose the correct statement about detecting if a linked list is circular using the two-pointer method.
AIf the fast pointer reaches null, the list is circular.
BIf the slow and fast pointers meet, the list contains a cycle.
CThe slow pointer moves two steps at a time, the fast pointer moves one step.
DThe method works only if the list has an even number of nodes.
Attempts:
2 left
💡 Hint
Think about what it means when two pointers meet inside the list.
🚀 Application
expert
3:00remaining
After detecting a cycle, how to find the start node of the cycle?
Given a linked list with a cycle detected by the two-pointer method, which approach correctly finds the node where the cycle begins?
ACount nodes until slow and fast meet, then move slow that many steps from head; the node reached is cycle start.
BMove fast pointer two steps and slow pointer one step until fast reaches null; the last slow node is the cycle start.
CReset slow to head, then move slow and fast one step at a time until they meet; the meeting node is the cycle start.
DReverse the linked list until slow and fast meet; the meeting node is the cycle start.
Attempts:
2 left
💡 Hint
After detecting the cycle, resetting one pointer to head and moving both one step at a time helps find the start.

Practice

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

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 A
  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
  • Confusing empty list with circular
  • Assuming single node list is always circular
2. Which TypeScript code snippet correctly checks if a linked list is circular using two pointers?
easy
A. for(let node = head; node != null; node = node.next) { if(node.next === head) return true; } return false;
B. let slow = head; while(slow) { slow = slow.next; if(slow === head) return true; } return false;
C. let slow = head; let fast = head; while(fast && fast.next) { slow = slow.next; fast = fast.next.next; if(slow === fast) return true; } return false;
D. let current = head; while(current.next) { current = current.next; } return current === null;

Solution

  1. Step 1: Understand Floyd's cycle detection

    Use two pointers: slow moves 1 step, fast moves 2 steps. If they meet, list is circular.
  2. Step 2: Analyze code snippets

    let slow = head; let fast = head; while(fast && fast.next) { slow = slow.next; fast = fast.next.next; if(slow === fast) return true; } return false; correctly implements this logic with proper conditions and pointer moves.
  3. Final Answer:

    let slow = head; let fast = head; while(fast && fast.next) { slow = slow.next; fast = fast.next.next; if(slow === fast) return true; } return false; -> Option C
  4. Quick Check:

    Two pointers meet means circular [OK]
Hint: Use slow and fast pointers to detect loop [OK]
Common Mistakes:
  • Moving fast pointer only one step
  • Not checking fast and fast.next for null
  • Returning false too early
3. What will be the output of this TypeScript code snippet?
class Node { constructor(public val: number, public next: Node | null = null) {} }
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = head.next;
function isCircular(head: Node | null): boolean {
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. true
B. false
C. undefined
D. Runtime error

Solution

  1. Step 1: Understand the linked list structure

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

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

    true -> Option A
  4. Quick Check:

    Loop exists, so output is true [OK]
Hint: Loop in list makes function return true [OK]
Common Mistakes:
  • Assuming no loop because list looks linear
  • Confusing node references
  • Ignoring pointer equality check
4. Identify the bug in this TypeScript function meant to detect a circular linked list:
function isCircular(head: Node | null): boolean {
let slow = head;
let fast = head;
while (fast !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
medium
A. The function should use a for loop instead of while
B. The slow pointer should move two steps instead of one
C. The function should return false inside the loop
D. The while loop should check fast and fast.next for null before moving pointers

Solution

  1. Step 1: Analyze pointer movement safety

    fast.next.next is accessed without checking if fast.next is null, risking runtime error.
  2. Step 2: Correct loop condition

    Loop should check both fast and fast.next are not null before moving pointers to avoid errors.
  3. Final Answer:

    The while loop should check fast and fast.next for null before moving pointers -> Option D
  4. Quick Check:

    Check fast and fast.next to avoid errors [OK]
Hint: Always check fast and fast.next before advancing fast [OK]
Common Mistakes:
  • Not checking fast.next before fast.next.next
  • Moving slow pointer incorrectly
  • Returning false too early
5. Given a singly linked list where the last node might point to any node in the list or null, how would you modify the detection function to also return the node where the cycle begins if it is circular?
Which approach correctly finds the start of the loop?
hard
A. Count the number of nodes and return the middle node as the start of the loop
B. After detecting the cycle, reset one pointer to head and move both pointers one step at a time until they meet; the meeting point is the start of the loop
C. Use a hash set to store visited nodes and return the first repeated node found
D. Reverse the linked list and check if the head changes to detect the loop start

Solution

  1. Step 1: Detect cycle using two pointers

    Use Floyd's algorithm to find if slow and fast pointers meet inside the loop.
  2. Step 2: Find loop start node

    Reset one pointer to head, move both pointers one step at a time; their meeting point is the loop start.
  3. Final Answer:

    After detecting the cycle, reset one pointer to head and move both pointers one step at a time until they meet; the meeting point is the start of the loop -> Option B
  4. Quick Check:

    Reset pointer and move both to find loop start [OK]
Hint: Reset pointer after cycle detection to find loop start [OK]
Common Mistakes:
  • Assuming middle node is loop start
  • Using node count instead of pointers
  • Trying to reverse list to find loop
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