Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this linked list traversal code?
Consider a singly linked list with nodes containing values 10, 20, 30 in that order. What will be printed after running the traversal code below?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); let current = head; let output = ''; while (current !== null) { output += current.value + ' -> '; current = current.next; } output += 'null'; console.log(output);
Attempts:
2 left
💡 Hint
Think about how the while loop moves through each node until it reaches the end.
✗ Incorrect
The code starts at the head node and moves through each node using the next pointer, appending each value followed by ' -> '. When current becomes null, it appends 'null' to indicate the end.
❓ Predict Output
intermediateWhat is the output after modifying the linked list before traversal?
Given the linked list 5 -> 15 -> 25 -> null, what will be printed after running the code below?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const head = new Node(5); head.next = new Node(15); head.next.next = new Node(25); // Insert a new node with value 10 between 5 and 15 const newNode = new Node(10); newNode.next = head.next; head.next = newNode; let current = head; let output = ''; while (current !== null) { output += current.value + ' -> '; current = current.next; } output += 'null'; console.log(output);
Attempts:
2 left
💡 Hint
Check how the new node is linked between existing nodes.
✗ Incorrect
The new node with value 10 is inserted between 5 and 15 by adjusting pointers, so traversal prints 5 -> 10 -> 15 -> 25 -> null.
🔧 Debug
advancedWhy does this linked list traversal code cause an infinite loop?
Examine the code below. It is supposed to print all node values in the list 1 -> 2 -> 3 -> null, but it runs forever. What is the cause?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); // Introduce a cycle head.next.next.next = head.next; let current = head; while (current !== null) { console.log(current.value); current = current.next; }
Attempts:
2 left
💡 Hint
Check the next pointers carefully for loops.
✗ Incorrect
The code creates a cycle by pointing the last node back to the second node, so current never becomes null and the loop runs forever.
🧠 Conceptual
advancedHow many nodes are printed by this traversal code?
Given a linked list with nodes 7 -> 14 -> 21 -> 28 -> null, what is the number of nodes printed by the code below?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const head = new Node(7); head.next = new Node(14); head.next.next = new Node(21); head.next.next.next = new Node(28); let current = head; let count = 0; while (current !== null) { console.log(current.value); count++; current = current.next; } console.log('Total nodes:', count);
Attempts:
2 left
💡 Hint
Count each node visited until current is null.
✗ Incorrect
The traversal visits all 4 nodes before current becomes null, so count is 4.
❓ Predict Output
expertWhat is the output of this recursive linked list print function?
Given the linked list 2 -> 4 -> 6 -> null, what will be printed by the recursive function below?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } const head = new Node(2); head.next = new Node(4); head.next.next = new Node(6); function printList(node: Node | null): void { if (node === null) { console.log('null'); return; } process.stdout.write(node.value + ' -> '); printList(node.next); } printList(head);
Attempts:
2 left
💡 Hint
Follow the recursive calls and how output is printed before recursion.
✗ Incorrect
The function prints the current node's value followed by ' -> ', then recursively prints the next nodes. When it reaches null, it prints 'null'.
