Bird
0
0
DSA Cprogramming~20 mins

Clone Linked List with Random Pointer in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Pointer Cloning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Cloning a Simple 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 clone function on a 3-node list?
DSA C
struct Node {
    int val;
    struct Node* next;
    struct Node* random;
};

// Assume cloneList function correctly clones the list
// Original list setup:
// Node1(val=1) -> Node2(val=2) -> Node3(val=3) -> NULL
// Random pointers: Node1.random = Node3, Node2.random = Node1, Node3.random = Node2

// After cloning, print cloned list as: val(random_val) -> ...

void printList(struct Node* head) {
    while (head) {
        printf("%d(%d) -> ", head->val, head->random ? head->random->val : -1);
        head = head->next;
    }
    printf("NULL\n");
}

int main() {
    // Setup original list and clone it
    // Then print cloned list
}
A1(1) -> 2(2) -> 3(3) -> NULL
B1(3) -> 2(1) -> 3(2) -> NULL
C1(-1) -> 2(-1) -> 3(-1) -> NULL
D1(2) -> 2(3) -> 3(1) -> NULL
Attempts:
2 left
💡 Hint
Trace the random pointers carefully after cloning.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Purpose of Interleaving Nodes in Cloning
Why do we interleave cloned nodes between original nodes in the common cloning algorithm for a linked list with random pointers?
ATo remove duplicate nodes from the list
BTo sort the list nodes by their values
CTo easily assign random pointers of cloned nodes without extra space
DTo reverse the linked list in place
Attempts:
2 left
💡 Hint
Think about how random pointers are assigned without using a hash map.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Cloning Random Pointers
What error will occur when running this snippet that tries to assign random pointers in the cloned list? for (Node* curr = head; curr != NULL; curr = curr->next->next) { if (curr->random != NULL) { curr->next->random = curr->random->next; } }
DSA C
for (Node* curr = head; curr != NULL; curr = curr->next->next) {
    if (curr->random != NULL) {
        curr->next->random = curr->random->next;
    }
}
ASegmentation fault due to curr->next being NULL
BNo error, code runs correctly
CCompilation error: 'random' is not a member of Node
DInfinite loop due to incorrect loop condition
Attempts:
2 left
💡 Hint
Check if curr->next can be NULL inside the loop.
Predict Output
advanced
2:00remaining
Output After Separating Cloned List from Interleaved List
Given an interleaved list of original and cloned nodes, what is the output of printing the cloned list values after separation?
DSA C
struct Node {
    int val;
    struct Node* next;
    struct Node* random;
};

// Interleaved list: 1 -> 1' -> 2 -> 2' -> 3 -> 3' -> NULL
// After separation, print cloned list values:
void printCloned(struct Node* head) {
    struct Node* curr = head;
    while (curr) {
        printf("%d -> ", curr->val);
        curr = curr->next;
    }
    printf("NULL\n");
}

int main() {
    // Assume separation done correctly
    // head points to cloned list head
    printCloned(head);
}
A3 -> 2 -> 1 -> NULL
B1 -> 1 -> 2 -> 2 -> 3 -> 3 -> NULL
CNULL
D1 -> 2 -> 3 -> NULL
Attempts:
2 left
💡 Hint
The cloned list contains only cloned nodes after separation.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Cloning a Linked List with Random Pointer
What is the overall time complexity of cloning a linked list with random pointers using the interleaving method?
AO(n), where n is the number of nodes
BO(n^2), due to nested loops
CO(1), constant time
DO(log n), because of binary search
Attempts:
2 left
💡 Hint
Consider how many times each node is visited.