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 code?
DSA Javascript
class Node { constructor(val, next = null, random = null) { this.val = val; this.next = next; this.random = random; } } function cloneLinkedList(head) { if (!head) return null; let curr = head; // Step 1: Insert cloned nodes while (curr) { let newNode = new Node(curr.val); newNode.next = curr.next; curr.next = newNode; curr = newNode.next; } // Step 2: Assign random pointers curr = head; while (curr) { if (curr.random) { curr.next.random = curr.random.next; } curr = curr.next.next; } // Step 3: Separate cloned list curr = head; let cloneHead = head.next; while (curr) { let clone = curr.next; curr.next = clone.next; clone.next = clone.next ? clone.next.next : null; curr = curr.next; } return cloneHead; } // Original list setup const node1 = new Node(1); const node2 = new Node(2); const node3 = new Node(3); node1.next = node2; node2.next = node3; node1.random = node3; node2.random = node1; node3.random = node2; const clonedHead = cloneLinkedList(node1); // Print cloned list values and their random values let output = []; let temp = clonedHead; while (temp) { output.push({ val: temp.val, random: temp.random ? temp.random.val : null }); temp = temp.next; } console.log(output);
Attempts:
2 left
💡 Hint
Focus on how the random pointers are assigned in the cloned list using the original nodes' random pointers.
✗ 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. This preserves the random links correctly in the cloned list.
🧠 Conceptual
intermediateUnderstanding the purpose of interleaving nodes in cloning
Why do we insert cloned nodes between original nodes in the cloning process of a linked list with random pointers?
Attempts:
2 left
💡 Hint
Think about how the random pointers are linked between original and cloned nodes.
✗ Incorrect
Interleaving cloned nodes allows direct access to the cloned node corresponding to any original node's random pointer by using originalNode.random.next. This avoids using extra memory like hash maps.
🔧 Debug
advancedIdentify the bug in this cloning code snippet
What error or issue will occur when running this code snippet for cloning a linked list with random pointers?
DSA Javascript
function cloneLinkedList(head) {
if (!head) return null;
let curr = head;
while (curr) {
let newNode = new Node(curr.val);
newNode.next = curr.next;
curr.next = newNode;
curr = curr.next;
}
// Missing step to assign random pointers
curr = head;
let cloneHead = head.next;
while (curr) {
let clone = curr.next;
curr.next = clone.next;
clone.next = clone.next ? clone.next.next : null;
curr = curr.next;
}
return cloneHead;
}Attempts:
2 left
💡 Hint
Check if the random pointers are assigned in the cloned nodes.
✗ Incorrect
The code misses the step where cloned nodes' random pointers are assigned. Without this, all cloned nodes have random set to null.
❓ Predict Output
advancedOutput after cloning a single-node list with self-random pointer
What is the output of the cloned list when the original list has one node whose random pointer points to itself?
DSA Javascript
class Node { constructor(val, next = null, random = null) { this.val = val; this.next = next; this.random = random; } } function cloneLinkedList(head) { if (!head) return null; let curr = head; while (curr) { let newNode = new Node(curr.val); newNode.next = curr.next; curr.next = newNode; curr = newNode.next; } curr = head; while (curr) { if (curr.random) { curr.next.random = curr.random.next; } curr = curr.next.next; } curr = head; let cloneHead = head.next; while (curr) { let clone = curr.next; curr.next = clone.next; clone.next = clone.next ? clone.next.next : null; curr = curr.next; } return cloneHead; } const node = new Node(10); node.random = node; const cloned = cloneLinkedList(node); let output = []; let temp = cloned; while (temp) { output.push({ val: temp.val, random: temp.random ? temp.random.val : null }); temp = temp.next; } console.log(output);
Attempts:
2 left
💡 Hint
Check how the random pointer of the single node is cloned.
✗ Incorrect
The cloned node's random pointer points to itself because original node's random points to itself, so cloned node's random is original.random.next which is cloned node itself.
🧠 Conceptual
expertTime and space complexity of cloning linked list with random pointer
What are the time and space complexities of the optimal algorithm that clones a linked list with random pointers by interleaving cloned nodes?
Attempts:
2 left
💡 Hint
Consider how many times the list is traversed and if extra data structures are used.
✗ Incorrect
The interleaving method traverses the list a constant number of times and uses no extra space besides the cloned nodes, so time is linear and space is constant.
