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 following C++ code that clones a linked list with random pointers? The print format is: (random_val) -> ... -> null
DSA C++
struct Node {
int val;
Node* next;
Node* random;
Node(int _val) : val(_val), next(nullptr), random(nullptr) {}
};
#include <iostream>
#include <unordered_map>
Node* cloneList(Node* head) {
if (!head) return nullptr;
std::unordered_map<Node*, Node*> map;
Node* curr = head;
while (curr) {
map[curr] = new Node(curr->val);
curr = curr->next;
}
curr = head;
while (curr) {
map[curr]->next = map[curr->next];
map[curr]->random = map[curr->random];
curr = curr->next;
}
return map[head];
}
void printList(Node* head) {
while (head) {
std::cout << head->val << "(";
if (head->random) std::cout << head->random->val;
else std::cout << "null";
std::cout << ") -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
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 = nullptr;
Node* cloned = cloneList(n1);
printList(cloned);
return 0;
}Attempts:
2 left
💡 Hint
Trace the mapping of original nodes to cloned nodes and how random pointers are assigned.
✗ Incorrect
The cloneList function creates new nodes for each original node and uses a map to assign next and random pointers correctly. The original list has random pointers: 1->3, 2->1, 3->null, so the cloned list prints the same pattern.
🧠 Conceptual
intermediateNumber of Nodes in Cloned List
If the original linked list has 5 nodes, how many nodes will the cloned linked list have after cloning with random pointers?
Attempts:
2 left
💡 Hint
Each original node corresponds to exactly one cloned node.
✗ Incorrect
The cloning process creates exactly one new node for each original node, regardless of random pointers. So the cloned list has the same number of nodes as the original.
🔧 Debug
advancedIdentify the Error in Cloning Code
What error will the following code produce when cloning a linked list with random pointers?
DSA C++
Node* cloneList(Node* head) {
if (!head) return nullptr;
Node* curr = head;
while (curr) {
Node* 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->next;
curr = curr->next->next;
}
curr = head;
Node* copyHead = head->next;
while (curr) {
Node* copy = curr->next;
curr->next = copy->next;
if (copy->next) copy->next = copy->next->next;
curr = curr->next;
}
return copyHead;
}
int main() {
Node* n1 = new Node(1);
n1->random = n1;
Node* cloned = cloneList(n1);
std::cout << cloned->random->val << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check how random pointers are assigned in the interleaved list.
✗ Incorrect
This is the classic O(1) space cloning method. It correctly assigns random pointers and restores the original list. The output prints 1 as expected.
❓ Predict Output
advancedOutput of Cloning with Null Random Pointers
What is the output of this code that clones a linked list where all random pointers are null? The print format is: (random_val) -> ... -> null
DSA C++
struct Node {
int val;
Node* next;
Node* random;
Node(int _val) : val(_val), next(nullptr), random(nullptr) {}
};
#include <iostream>
#include <unordered_map>
Node* cloneList(Node* head) {
if (!head) return nullptr;
std::unordered_map<Node*, Node*> map;
Node* curr = head;
while (curr) {
map[curr] = new Node(curr->val);
curr = curr->next;
}
curr = head;
while (curr) {
map[curr]->next = map[curr->next];
map[curr]->random = map[curr->random];
curr = curr->next;
}
return map[head];
}
void printList(Node* head) {
while (head) {
std::cout << head->val << "(";
if (head->random) std::cout << head->random->val;
else std::cout << "null";
std::cout << ") -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* n1 = new Node(10);
Node* n2 = new Node(20);
Node* n3 = new Node(30);
n1->next = n2; n2->next = n3;
n1->random = nullptr; n2->random = nullptr; n3->random = nullptr;
Node* cloned = cloneList(n1);
printList(cloned);
return 0;
}Attempts:
2 left
💡 Hint
Random pointers are all null, so cloned nodes have random as null.
✗ Incorrect
Since all random pointers are null in the original list, the cloned list will also have random pointers as null for all nodes.
🧠 Conceptual
expertSpace Complexity of Cloning Methods
Which cloning method uses O(1) extra space when cloning a linked list with random pointers?
Attempts:
2 left
💡 Hint
Think about which method avoids extra data structures.
✗ Incorrect
The interleaving method inserts cloned nodes between original nodes, uses existing pointers to assign random pointers, and then separates the lists, achieving O(1) extra space.
