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 linked list with random pointers
What is the printed output of the cloned linked list's values and their random pointers after running the given Java code?
DSA Java
class Node { int val; Node next; Node random; Node(int val) { this.val = val; this.next = null; this.random = null; } } public class Solution { public Node copyRandomList(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 pseudoHead = new Node(0); Node copyCurr = pseudoHead; while (curr != null) { copyCurr.next = curr.next; curr.next = curr.next.next; curr = curr.next; copyCurr = copyCurr.next; } return pseudoHead.next; } public static void main(String[] args) { Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); n1.next = n2; n2.next = n3; n1.random = n3; n2.random = n1; n3.random = n2; Solution sol = new Solution(); Node cloned = sol.copyRandomList(n1); Node temp = cloned; while (temp != null) { System.out.print(temp.val + "(" + (temp.random != null ? temp.random.val : "null") + ") -> "); temp = temp.next; } System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Trace the cloning steps: first interleaving nodes, then assigning random pointers, then separating the lists.
✗ Incorrect
The code first creates a copy node after each original node, then assigns the random pointers of the copied nodes by referencing the original nodes' random pointers' next nodes. Finally, it separates the copied list from the original. The random pointers in the cloned list point to the cloned nodes corresponding to the original random targets.
🧠 Conceptual
intermediateUnderstanding the purpose of interleaving nodes in cloning
Why does the cloning algorithm insert copied nodes immediately after their original nodes in the linked list?
Attempts:
2 left
💡 Hint
Think about how the random pointer of a copied node can be set without extra space.
✗ Incorrect
By placing the copied node right after the original, the copied node's random pointer can be set by accessing the original node's random pointer's next node, which is the copied node of the random target. This avoids using extra memory like hash maps.
🔧 Debug
advancedIdentify the error in this cloning code snippet
What error will occur when running this code snippet for cloning a linked list with random pointers?
DSA Java
Node curr = head;
while (curr != null) {
Node copy = new Node(curr.val);
copy.next = curr.next;
curr.next = copy;
curr = curr.next;
}
Attempts:
2 left
💡 Hint
Check how curr is updated inside the loop.
✗ Incorrect
Inside the loop, curr is updated to curr.next, which is the newly inserted copy node. The next iteration sets copy.next to curr.next again, causing curr to never move past the copy nodes, resulting in an infinite loop.
❓ Predict Output
advancedOutput after cloning and modifying original list
Given the cloning method, what is the output after modifying the original list's random pointer post-cloning?
DSA Java
Node n1 = new Node(1); Node n2 = new Node(2); n1.next = n2; n1.random = n2; n2.random = n1; Solution sol = new Solution(); Node cloned = sol.copyRandomList(n1); n1.random = null; Node temp = cloned; while (temp != null) { System.out.print(temp.val + "(" + (temp.random != null ? temp.random.val : "null") + ") -> "); temp = temp.next; } System.out.print("null");
Attempts:
2 left
💡 Hint
The cloned list is independent of changes to the original list after cloning.
✗ Incorrect
The cloned list's random pointers are set during cloning and do not change when the original list's random pointers are modified later. So the cloned list retains the original random pointer structure.
🚀 Application
expertHow to optimize cloning a linked list with random pointers for space
Which approach uses O(1) extra space to clone a linked list with random pointers?
Attempts:
2 left
💡 Hint
Think about how to avoid extra data structures.
✗ Incorrect
Interleaving nodes allows setting random pointers without extra memory. Hash maps use O(n) space. Searching for random targets repeatedly is O(n^2). Recursion can cause stack overflow and does not reduce space complexity.
