Challenge - 5 Problems
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linked List Node Traversal
What is the printed output of the following C# code that creates and traverses a singly linked list?
DSA C#
public class Node { public int Value; public Node Next; public Node(int val) { Value = val; Next = null; } } public class Program { public static void Main() { Node head = new Node(1); head.Next = new Node(2); head.Next.Next = new Node(3); Node current = head; while (current != null) { System.Console.Write(current.Value); if (current.Next != null) System.Console.Write(" -> "); current = current.Next; } System.Console.WriteLine(" -> null"); } }
Attempts:
2 left
💡 Hint
Trace the nodes starting from head and follow the Next pointers.
✗ Incorrect
The code creates nodes with values 1, 2, and 3 linked in order. Traversing from head prints values in sequence: 1, then 2, then 3, followed by null.
❓ Predict Output
intermediateOutput after Reversing a Singly Linked List
Given the following C# code that reverses a singly linked list, what is the printed output?
DSA C#
public class Node { public int Value; public Node Next; public Node(int val) { Value = val; Next = null; } } public class Program { public static Node Reverse(Node head) { Node prev = null; Node current = head; while (current != null) { Node nextTemp = current.Next; current.Next = prev; prev = current; current = nextTemp; } return prev; } public static void Main() { Node head = new Node(1); head.Next = new Node(2); head.Next.Next = new Node(3); Node reversedHead = Reverse(head); Node current = reversedHead; while (current != null) { System.Console.Write(current.Value); if (current.Next != null) System.Console.Write(" -> "); current = current.Next; } System.Console.WriteLine(" -> null"); } }
Attempts:
2 left
💡 Hint
Reversing changes the direction of Next pointers.
✗ Incorrect
The Reverse method flips the Next pointers so the last node becomes the new head. The printed output is the reversed list: 3 -> 2 -> 1 -> null.
🔧 Debug
advancedIdentify the Error in Doubly Linked List Node Insertion
What error will occur when running the following C# code that inserts a new node after the head in a doubly linked list?
DSA C#
public class Node { public int Value; public Node Prev; public Node Next; public Node(int val) { Value = val; Prev = null; Next = null; } } public class Program { public static void Main() { Node head = new Node(1); Node second = new Node(2); head.Next = second; Node newNode = new Node(3); newNode.Next = head.Next; newNode.Prev = head; head.Next = newNode; // Missing update of newNode.Next.Prev System.Console.WriteLine(head.Next.Value); System.Console.WriteLine(newNode.Next.Prev.Value); } }
Attempts:
2 left
💡 Hint
Check if all Prev pointers are updated after insertion.
✗ Incorrect
The code forgets to update newNode.Next.Prev to newNode. So newNode.Next.Prev is still null, but after insertion, it should point to newNode. Accessing newNode.Next.Prev.Value causes NullReferenceException.
🧠 Conceptual
advancedUnderstanding Circular Linked List Node Connections
In a circular singly linked list with three nodes A, B, and C connected in order, what is the value of the node reached by starting at A and following Next pointers 4 times?
Attempts:
2 left
💡 Hint
Remember the list loops back to the start after the last node.
✗ Incorrect
Starting at A: 1st Next is B, 2nd Next is C, 3rd Next is A (loop), 4th Next is B again.
🚀 Application
expertDetermine the Number of Nodes in a Complex Linked Structure
Given the following C# code that creates nodes and links them with Next pointers, how many unique nodes are reachable starting from node1 by following Next pointers until null?
DSA C#
public class Node { public int Value; public Node Next; public Node(int val) { Value = val; Next = null; } } public class Program { public static void Main() { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); node1.Next = node2; node2.Next = node3; node3.Next = node2; // cycle here node4.Next = null; // Count nodes reachable from node1 int count = 0; Node current = node1; System.Collections.Generic.HashSet<Node> visited = new System.Collections.Generic.HashSet<Node>(); while (current != null && !visited.Contains(current)) { visited.Add(current); count++; current = current.Next; } System.Console.WriteLine(count); } }
Attempts:
2 left
💡 Hint
Consider the cycle and how visited nodes prevent infinite counting.
✗ Incorrect
Starting at node1: visit node1, then node2, then node3. Next of node3 is node2 which is already visited, so loop stops. Total unique nodes counted: 3.
