Bird
0
0
DSA Cprogramming~20 mins

Insert at Middle Specific Position in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inserting at middle position in linked list
What is the printed linked list after inserting a node with value 99 at position 3?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void insertAtPosition(struct Node** head, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    if (position == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    for (int i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        free(newNode);
        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 = (struct Node*)malloc(sizeof(struct Node));
    head->data = 1;
    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 2;
    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 3;
    head->next->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->next->data = 4;
    head->next->next->next->next = NULL;

    insertAtPosition(&head, 99, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 99 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 99 -> 4 -> null
C1 -> 99 -> 2 -> 3 -> 4 -> null
D99 -> 1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
Predict Output
intermediate
2:00remaining
Result of inserting at position beyond list length
What is the output after trying to insert value 50 at position 10 in the list 10 -> 20 -> 30 -> null?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void insertAtPosition(struct Node** head, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    if (position == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    for (int i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        free(newNode);
        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 = (struct Node*)malloc(sizeof(struct Node));
    head->data = 10;
    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 20;
    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 30;
    head->next->next->next = NULL;

    insertAtPosition(&head, 50, 10);
    printList(head);
    return 0;
}
A10 -> 50 -> 20 -> 30 -> null
B10 -> 20 -> 30 -> null
C10 -> 20 -> 30 -> 50 -> null
D50 -> 10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the insertion does not happen.
🔧 Debug
advanced
2:00remaining
Identify the bug in insert at middle position function
What is the bug in this insertAtPosition function that causes a crash when inserting at position 3 in an empty list?
DSA C
void insertAtPosition(struct Node** head, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    if (position == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    for (int i = 1; i < position - 1; i++) {
        temp = temp->next;
    }
    newNode->next = temp->next;
    temp->next = newNode;
}
AThe loop does not check if temp is NULL before accessing temp->next, causing a crash.
BThe newNode is not allocated memory before use.
CThe function does not update the head pointer when position is 1.
DThe function frees newNode before inserting it.
Attempts:
2 left
💡 Hint
Think about what happens when the list is empty and position is 3.
🧠 Conceptual
advanced
1:30remaining
Time complexity of inserting at middle position in singly linked list
What is the time complexity of inserting a node at a specific middle position in a singly linked list of size n?
AO(1) because insertion is always at the head.
BO(log n) because of binary search to find the position.
CO(n) because you must traverse the list to the position before insertion.
DO(n^2) because each insertion requires traversing the entire list.
Attempts:
2 left
💡 Hint
Think about how you find the position to insert in a singly linked list.
🚀 Application
expert
3:00remaining
Result of multiple insertions at various middle positions
Given an initially empty singly linked list, after performing these insertions in order: 1) Insert 10 at position 1 2) Insert 20 at position 2 3) Insert 15 at position 2 4) Insert 5 at position 1 5) Insert 25 at position 5 What is the final printed linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void insertAtPosition(struct Node** head, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    if (position == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* temp = *head;
    for (int i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }
    if (temp == NULL) {
        free(newNode);
        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 = NULL;
    insertAtPosition(&head, 10, 1);
    insertAtPosition(&head, 20, 2);
    insertAtPosition(&head, 15, 2);
    insertAtPosition(&head, 5, 1);
    insertAtPosition(&head, 25, 5);
    printList(head);
    return 0;
}
A5 -> 15 -> 10 -> 20 -> 25 -> null
B10 -> 5 -> 15 -> 20 -> 25 -> null
C5 -> 10 -> 20 -> 15 -> 25 -> null
D5 -> 10 -> 15 -> 20 -> 25 -> null
Attempts:
2 left
💡 Hint
Insertions shift existing nodes to the right from the insertion position.