Bird
0
0
DSA Cprogramming~20 mins

Linked List vs Array When to Choose Which in DSA C - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List vs Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When is a linked list better than an array?
Which scenario best shows when you should choose a linked list over an array?
AWhen you want to sort elements quickly using index-based algorithms.
BWhen you need fast random access to elements by index.
CWhen you want to use less memory overall.
DWhen you frequently insert or delete elements in the middle of the collection.
Attempts:
2 left
💡 Hint
Think about which data structure handles insertions and deletions efficiently.
🧠 Conceptual
intermediate
2:00remaining
Why might arrays be preferred over linked lists?
Which reason best explains why arrays are often preferred over linked lists?
AArrays use pointers to connect elements, making traversal slower.
BArrays can easily grow and shrink without reallocating memory.
CArrays allow fast access to elements by index, improving speed for lookups.
DArrays use more memory per element due to storing extra pointers.
Attempts:
2 left
💡 Hint
Think about how you access elements in an array versus a linked list.
Predict Output
advanced
3:00remaining
Output after inserting in a linked list
What is the printed state of the linked list after inserting 99 at position 2?
DSA C
struct Node {
    int data;
    struct Node* next;
};

void insertAtPosition(struct Node** head, int data, int pos) {
    struct Node* newNode = malloc(sizeof(struct Node));
    newNode->data = data;
    if (pos == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    for (int i = 1; i < pos - 1 && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) return;
    newNode->next = temp->next;
    temp->next = newNode;
}

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

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 10;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 20;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 30;
    head->next->next->next = NULL;

    insertAtPosition(&head, 99, 2);
    printList(head);
    return 0;
}
A10 -> 20 -> 30 -> 99 -> null
B10 -> 99 -> 20 -> 30 -> null
C99 -> 10 -> 20 -> 30 -> null
D10 -> 20 -> 99 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember position 2 means after the first element.
Predict Output
advanced
3:00remaining
Array resizing behavior
What is the content of the array after doubling its size and copying elements?
DSA C
int* resizeArray(int* arr, int oldSize, int newSize) {
    int* newArr = malloc(newSize * sizeof(int));
    for (int i = 0; i < oldSize; i++) {
        newArr[i] = arr[i];
    }
    for (int i = oldSize; i < newSize; i++) {
        newArr[i] = 0;
    }
    free(arr);
    return newArr;
}

int main() {
    int oldSize = 3;
    int* arr = malloc(oldSize * sizeof(int));
    arr[0] = 5;
    arr[1] = 10;
    arr[2] = 15;

    int newSize = 6;
    arr = resizeArray(arr, oldSize, newSize);

    for (int i = 0; i < newSize; i++) {
        printf("%d ", arr[i]);
    }
    free(arr);
    return 0;
}
A5 10 15 0 0 0
B5 10 15 15 15 15
C0 0 0 0 0 0
D5 10 15 garbage garbage garbage
Attempts:
2 left
💡 Hint
New elements after oldSize are set to zero.
🧠 Conceptual
expert
3:00remaining
Choosing data structure for mixed operations
You need a data structure for a program that requires frequent insertions and deletions anywhere, but also fast access by index sometimes. Which is the best choice?
AUse a balanced tree or a specialized data structure like a skip list to balance both needs.
BUse an array because it provides fast access by index, and insertions/deletions are rare.
CUse a stack because it supports fast insertions and deletions at one end.
DUse a linked list because it handles insertions and deletions efficiently, and access speed is not important.
Attempts:
2 left
💡 Hint
Think about a data structure that balances both fast access and efficient insertions/deletions.