Bird
0
0
DSA Cprogramming~20 mins

Search for a Value in Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this linked list search code?
Given the linked list and search function below, what will be printed when searching for value 3?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

int search(Node* head, int val) {
    Node* current = head;
    while (current != NULL) {
        if (current->data == val) {
            return 1;
        }
        current = current->next;
    }
    return 0;
}

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

    if (search(head, 3)) {
        printf("Found\n");
    } else {
        printf("Not Found\n");
    }
    return 0;
}
AFound
BRuntime Error
CCompilation Error
DNot Found
Attempts:
2 left
💡 Hint
Trace the linked list nodes and check if the value 3 exists.
Predict Output
intermediate
2:00remaining
What is the output when searching for a value not in the list?
What will the program print when searching for value 5 in the linked list below?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

int search(Node* head, int val) {
    Node* current = head;
    while (current != NULL) {
        if (current->data == val) {
            return 1;
        }
        current = current->next;
    }
    return 0;
}

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

    if (search(head, 5)) {
        printf("Found\n");
    } else {
        printf("Not Found\n");
    }
    return 0;
}
AFound
BCompilation Error
CNot Found
DSegmentation Fault
Attempts:
2 left
💡 Hint
Check if 5 is present in the list nodes.
🔧 Debug
advanced
2:00remaining
What error does this linked list search code cause?
What error will occur when running this code?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>

int search(Node* head, int val) {
    Node* current = head;
    while (current->next != NULL) {
        if (current->data == val) {
            return 1;
        }
        current = current->next;
    }
    return 0;
}

int main() {
    Node node3 = {3, NULL};
    Node node2 = {2, &node3};
    Node node1 = {1, &node2};

    if (search(&node1, 3)) {
        printf("Found\n");
    } else {
        printf("Not Found\n");
    }
    return 0;
}
ANo error, prints Found
BInfinite loop
CCompilation Error
DSegmentation Fault
Attempts:
2 left
💡 Hint
Look at the loop condition and what happens when current is the last node.
🧠 Conceptual
advanced
1:30remaining
Why is the loop condition 'while(current != NULL)' preferred in linked list search?
Choose the best reason why 'while(current != NULL)' is used instead of 'while(current->next != NULL)' in linked list search.
AIt prevents memory leaks in the linked list.
BIt ensures the last node is checked for the value.
CIt makes the code run faster by skipping the last node.
DIt avoids the need to initialize the head pointer.
Attempts:
2 left
💡 Hint
Think about whether the last node's data is checked.
🚀 Application
expert
2:30remaining
What is the output after searching for value 7 in this linked list?
Given the linked list created dynamically and the search function, what will be printed when searching for value 7?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

Node* createNode(int val) {
    Node* newNode = malloc(sizeof(Node));
    newNode->data = val;
    newNode->next = NULL;
    return newNode;
}

int search(Node* head, int val) {
    Node* current = head;
    while (current != NULL) {
        if (current->data == val) {
            return 1;
        }
        current = current->next;
    }
    return 0;
}

int main() {
    Node* head = createNode(5);
    head->next = createNode(7);
    head->next->next = createNode(9);

    if (search(head, 7)) {
        printf("Found\n");
    } else {
        printf("Not Found\n");
    }
    return 0;
}
AFound
BNot Found
CMemory Leak Detected
DSegmentation Fault
Attempts:
2 left
💡 Hint
Check if the value 7 is in any node's data.