Challenge - 5 Problems
Linked List vs Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When is a linked list better than an array?
Which scenario best shows when you should choose a linked list over an array?
Attempts:
2 left
💡 Hint
Think about which data structure handles insertions and deletions efficiently.
✗ Incorrect
Linked lists allow quick insertions and deletions anywhere because they just change pointers. Arrays require shifting elements, which is slower.
🧠 Conceptual
intermediate2:00remaining
Why might arrays be preferred over linked lists?
Which reason best explains why arrays are often preferred over linked lists?
Attempts:
2 left
💡 Hint
Think about how you access elements in an array versus a linked list.
✗ Incorrect
Arrays store elements contiguously, so accessing by index is very fast. Linked lists require walking through nodes one by one.
❓ Predict Output
advanced3: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;
}Attempts:
2 left
💡 Hint
Remember position 2 means after the first element.
✗ Incorrect
Inserting 99 at position 2 places it after the first node (10), so the list becomes 10 -> 99 -> 20 -> 30 -> null.
❓ Predict Output
advanced3: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; }
Attempts:
2 left
💡 Hint
New elements after oldSize are set to zero.
✗ Incorrect
The function copies old elements and sets new slots to zero, so the output is 5 10 15 0 0 0.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about a data structure that balances both fast access and efficient insertions/deletions.
✗ Incorrect
Neither arrays nor linked lists alone handle both requirements well. Balanced trees or skip lists provide a good compromise.
