Challenge - 5 Problems
Random Pointer Cloning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Cloning a Simple Linked List with Random Pointers
What is the printed output of the cloned linked list's nodes' values and their random pointers after running the given cloning code?
DSA C#
public class Node { public int val; public Node next; public Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; } } public Node CloneList(Node head) { if (head == null) return null; Node curr = head; while (curr != null) { Node copy = new Node(curr.val); copy.next = curr.next; curr.next = copy; curr = copy.next; } curr = head; while (curr != null) { if (curr.random != null) { curr.next.random = curr.random.next; } curr = curr.next.next; } curr = head; Node dummy = new Node(0); Node copyCurr = dummy; while (curr != null) { copyCurr.next = curr.next; curr.next = curr.next.next; curr = curr.next; copyCurr = copyCurr.next; } return dummy.next; } // Setup linked list: // Node1(val=1) -> Node2(val=2) -> null // Node1.random = Node2 // Node2.random = Node2 Node node1 = new Node(1); Node node2 = new Node(2); node1.next = node2; node1.random = node2; node2.random = node2; Node clonedHead = CloneList(node1); Node temp = clonedHead; while (temp != null) { Console.WriteLine($"Node val: {temp.val}, Random val: {(temp.random != null ? temp.random.val.ToString() : "null")}"); temp = temp.next; }
Attempts:
2 left
💡 Hint
Focus on how the random pointers are assigned in the cloned list after interleaving nodes.
✗ Incorrect
The cloning process creates new nodes interleaved with original nodes. The random pointer of each cloned node is set to the next node of the original node's random pointer. Since node1.random points to node2, cloned node1.random points to cloned node2. Similarly, node2.random points to itself, so cloned node2.random points to cloned node2.
❓ Predict Output
intermediateOutput of Cloning a Linked List with Null Random Pointers
What is the output when cloning a linked list where all random pointers are null?
DSA C#
Node node1 = new Node(10); Node node2 = new Node(20); Node node3 = new Node(30); node1.next = node2; node2.next = node3; // All random pointers are null Node clonedHead = CloneList(node1); Node temp = clonedHead; while (temp != null) { Console.WriteLine($"Node val: {temp.val}, Random val: {(temp.random != null ? temp.random.val.ToString() : "null")}"); temp = temp.next; }
Attempts:
2 left
💡 Hint
If original random pointers are null, cloned random pointers should also be null.
✗ Incorrect
Since all original nodes have random pointers set to null, the cloned nodes' random pointers remain null after cloning.
🔧 Debug
advancedIdentify the Bug in Cloning Random Pointer Assignment
Given the cloning code below, what error will occur when running it on a linked list with random pointers?
DSA C#
Node curr = head;
while (curr != null) {
curr.next.random = curr.random.next;
curr = curr.next.next;
}Attempts:
2 left
💡 Hint
Consider what happens if curr.random is null before accessing curr.random.next.
✗ Incorrect
If curr.random is null, accessing curr.random.next causes a NullReferenceException. The code should check if curr.random is not null before accessing curr.random.next.
🚀 Application
advancedNumber of Nodes in Cloned List After Cloning
If the original linked list has 5 nodes, how many nodes will the cloned linked list have after running the cloning algorithm?
Attempts:
2 left
💡 Hint
The cloned list is a separate copy with the same number of nodes as the original.
✗ Incorrect
The cloning algorithm creates exactly one new node for each original node, so the cloned list has the same number of nodes as the original.
🧠 Conceptual
expertWhy Interleaving Nodes Helps in Cloning Random Pointers
Why does the cloning algorithm insert cloned nodes between original nodes before assigning random pointers?
Attempts:
2 left
💡 Hint
Think about how to find the clone of a node's random pointer without using a dictionary.
✗ Incorrect
By placing cloned nodes right after their originals, the clone of any node's random pointer is simply the next node of that random pointer. This avoids using extra memory for mapping.
