Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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));
Attempts:
2 left
💡 Hint
Think about how the fast and slow pointers move and when they meet in a cycle.
✗ Incorrect
The fast pointer moves two steps and the slow pointer moves one step. Because there is a cycle (d.next = b), they will eventually meet, so the function returns true.
❓ Predict Output
intermediateWhat 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));
Attempts:
2 left
💡 Hint
No node points back to an earlier node, so no cycle exists.
✗ Incorrect
Since d.next is null, the fast pointer will reach the end without meeting the slow pointer, so the function returns false.
🔧 Debug
advancedWhat 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));
Attempts:
2 left
💡 Hint
Check how the code handles the end of the list when fast or fast.next is null.
✗ Incorrect
The code safely checks fast and fast.next before accessing next properties, so no error occurs. The list is not circular, so it returns false.
🧠 Conceptual
advancedWhich 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.
Attempts:
2 left
💡 Hint
Think about what it means when two pointers meet inside the list.
✗ Incorrect
When slow and fast pointers meet, it means the fast pointer has looped back and caught up with slow, indicating a cycle.
🚀 Application
expertAfter 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?
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.
✗ Incorrect
Resetting slow to head and moving both pointers one step at a time causes them to meet at the cycle start node.
