Challenge - 5 Problems
Random Pointer Clone 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 Typescript
class Node { val: number; next: Node | null; random: Node | null; constructor(val?: number, next?: Node | null, random?: Node | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; this.random = random === undefined ? null : random; } } function cloneLinkedList(head: Node | null): Node | null { if (!head) return null; // Step 1: Insert cloned nodes let curr: Node | null = head; while (curr) { const newNode = new Node(curr.val, curr.next, null); 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 lists curr = head; const dummy = new Node(0); let copyCurr = dummy; while (curr) { copyCurr.next = curr.next; curr.next = curr.next!.next; curr = curr.next; copyCurr = copyCurr.next!; } return dummy.next; } // Create original list 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 random values let temp = clonedHead; const result: string[] = []; while (temp) { const randomVal = temp.random ? temp.random.val : 'null'; result.push(`${temp.val}->random:${randomVal}`); temp = temp.next; } console.log(result.join(' -> '));
Attempts:
2 left
💡 Hint
Trace the cloning steps carefully, especially how random pointers are assigned.
✗ Incorrect
The cloned list nodes have the same values as original nodes. The random pointers in the clone point to the cloned nodes corresponding to the original random targets. So node 1's random points to node 3, node 2's random points to node 1, and node 3's random points to node 2.
🧠 Conceptual
intermediateUnderstanding the purpose of interleaving cloned nodes
Why do we insert cloned nodes immediately after their original nodes in the linked list during cloning with random pointers?
Attempts:
2 left
💡 Hint
Think about how random pointers can be assigned without extra space.
✗ Incorrect
By placing cloned nodes right after original nodes, we can access the clone of any node's random pointer by looking at original.random.next. This avoids using extra space like hash maps.
🔧 Debug
advancedIdentify the bug causing incorrect random pointers in cloned list
Given the following code snippet for cloning a linked list with random pointers, what is the bug causing cloned nodes to have incorrect random pointers?
DSA Typescript
function cloneRandomList(head: Node | null): Node | null {
if (!head) return null;
let curr = head;
while (curr) {
const copy = new Node(curr.val);
copy.next = curr.next;
curr.next = copy;
curr = copy.next;
}
curr = head;
while (curr) {
if (curr.random) {
curr.next!.random = curr.random;
}
curr = curr.next!.next;
}
curr = head;
const dummy = new Node(0);
let copyCurr = dummy;
while (curr) {
copyCurr.next = curr.next;
curr.next = curr.next!.next;
curr = curr.next;
copyCurr = copyCurr.next!;
}
return dummy.next;
}Attempts:
2 left
💡 Hint
Check how random pointers of cloned nodes are assigned.
✗ Incorrect
The cloned node's random pointer must point to the clone of the original node's random target, which is curr.random.next, not curr.random itself.
❓ Predict Output
advancedOutput after cloning a linked list with some null random pointers
What is the output of the cloned linked list's values and their random pointers after running the code below?
DSA Typescript
class Node { val: number; next: Node | null; random: Node | null; constructor(val?: number, next?: Node | null, random?: Node | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; this.random = random === undefined ? null : random; } } function cloneList(head: Node | null): Node | null { if (!head) return null; let curr = head; while (curr) { const copy = new Node(curr.val, curr.next, null); curr.next = copy; curr = copy.next; } curr = head; while (curr) { if (curr.random) { curr.next!.random = curr.random.next; } curr = curr.next!.next; } curr = head; const dummy = new Node(0); let copyCurr = dummy; while (curr) { copyCurr.next = curr.next; curr.next = curr.next!.next; curr = curr.next; copyCurr = copyCurr.next!; } return dummy.next; } const n1 = new Node(10); const n2 = new Node(20); const n3 = new Node(30); const n4 = new Node(40); n1.next = n2; n2.next = n3; n3.next = n4; n1.random = null; n2.random = n4; n3.random = null; n4.random = n2; const cloned = cloneList(n1); let temp = cloned; const output: string[] = []; while (temp) { const r = temp.random ? temp.random.val : 'null'; output.push(`${temp.val}->random:${r}`); temp = temp.next; } console.log(output.join(' -> '));
Attempts:
2 left
💡 Hint
Check which original nodes have null random pointers and how they map in the clone.
✗ Incorrect
The cloned list preserves the original random pointer structure, including nulls. So nodes with null random remain null in clone; others point to the cloned target nodes.
🚀 Application
expertTime complexity of cloning a linked list with random pointers
What is the overall time complexity of cloning a linked list with random pointers using the interleaving method shown in the code examples?
Attempts:
2 left
💡 Hint
Consider how many times each node is visited during cloning.
✗ Incorrect
The algorithm visits each node a constant number of times: once to insert clones, once to assign random pointers, and once to separate lists, resulting in O(n) time.
