Bird
0
0
DSA Cprogramming~20 mins

Why Linked List Exists and What Problem It Solves in DSA C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why Use a Linked List Instead of an Array?

Imagine you want to store a list of items but you don't know how many items there will be, and you want to add or remove items often. Why might a linked list be better than an array in this case?

ABecause linked lists can grow and shrink easily without moving other items, while arrays have fixed size or need costly resizing.
BBecause arrays use pointers to connect items, making them slower to access than linked lists.
CBecause arrays cannot store numbers, only linked lists can.
DBecause linked lists store items in continuous memory, making them faster to access than arrays.
Attempts:
2 left
💡 Hint

Think about what happens when you add or remove items in an array versus a linked list.

Predict Output
intermediate
2:00remaining
Output of Adding Nodes in a Linked List

What will be the printed linked list after running this C code that adds nodes at the front?

DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

int main() {
    Node* head = NULL;
    for (int i = 1; i <= 3; i++) {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = i;
        newNode->next = head;
        head = newNode;
    }
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> NULL
BNULL -> 1 -> 2 -> 3
C3 -> 2 -> 1 -> NULL
D3 -> 1 -> 2 -> NULL
Attempts:
2 left
💡 Hint

Nodes are added at the front, so the last added node becomes the new head.

🔧 Debug
advanced
2:00remaining
Why Does This Linked List Code Crash?

Consider this code snippet that tries to traverse a linked list. Why does it crash?

DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

void traverse(Node* head) {
    while (head->next != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}
ABecause the loop condition skips printing the last node, and accessing head->next when head is NULL causes a crash.
BBecause the code uses printf incorrectly without a format specifier.
CBecause the struct Node is missing a semicolon after its definition.
DBecause the function traverse is missing a return statement.
Attempts:
2 left
💡 Hint

Think about what happens when the list has only one node or when head becomes NULL.

Predict Output
advanced
2:00remaining
Result of Removing a Node from a Linked List

What is the linked list after removing the node with value 2 from this list?

DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

Node* removeNode(Node* head, int val) {
    Node* temp = head;
    Node* prev = NULL;
    while (temp != NULL && temp->data != val) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return head;
    if (prev == NULL) {
        head = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
    return head;
}

int main() {
    Node* head = (Node*)malloc(sizeof(Node));
    head->data = 1;
    head->next = (Node*)malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

    head = removeNode(head, 2);
    printList(head);
    return 0;
}
A1 -> NULL
B1 -> 2 -> 3 -> NULL
C2 -> 3 -> NULL
D1 -> 3 -> NULL
Attempts:
2 left
💡 Hint

Removing node with value 2 means it should no longer appear in the list.

🧠 Conceptual
expert
2:30remaining
Why Linked Lists Solve Memory Fragmentation Problems

Memory fragmentation happens when free memory is split into small pieces scattered around. How does a linked list help solve this problem compared to arrays?

ABecause linked lists use continuous memory blocks, they prevent fragmentation by keeping data together.
BBecause linked lists store nodes anywhere in memory and connect them with pointers, they do not require continuous memory blocks like arrays do.
CBecause arrays can grow dynamically without moving data, they solve fragmentation better than linked lists.
DBecause linked lists store all data in a single large block, they avoid fragmentation.
Attempts:
2 left
💡 Hint

Think about how linked list nodes are stored in memory compared to arrays.