Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of searching for a value in a linked list?
easy
A. To find if a specific value exists in the list
B. To add a new node at the end of the list
C. To delete the first node of the list
D. To reverse the order of nodes in the list

Solution

  1. Step 1: Understand the search operation

    Searching means checking each node to see if it contains the value we want.
  2. Step 2: Identify the goal of searching

    The goal is to find whether the value exists anywhere in the linked list.
  3. Final Answer:

    To find if a specific value exists in the list -> Option A
  4. Quick Check:

    Search = Find value in list [OK]
Hint: Search means look for a value in nodes [OK]
Common Mistakes:
  • Confusing search with insertion or deletion
  • Thinking search changes the list
  • Assuming search reverses the list
2. Which of the following is the correct way to start searching a linked list in C?
easy
A. Node* current = head; while(current != NULL) { if(current->data == value) break; current = current->next; }
B. Node* current = NULL; while(current != head) { if(current->data == value) break; current = current->next; }
C. Node* current = head; while(current->next != NULL) { if(current->data == value) break; current = current->next; }
D. Node* current = head; for(int i=0; inext; }

Solution

  1. Step 1: Initialize pointer to head

    We start searching from the head node, so current = head is correct.
  2. Step 2: Traverse until end or value found

    Loop continues while current is not NULL, checking each node's data.
  3. Final Answer:

    Node* current = head; while(current != NULL) { if(current->data == value) break; current = current->next; } -> Option A
  4. Quick Check:

    Start at head and loop till NULL [OK]
Hint: Start at head, loop until NULL or value found [OK]
Common Mistakes:
  • Initializing current to NULL instead of head
  • Looping until current->next is NULL misses last node
  • Using for loop with value as count incorrectly
3. What will be the output of the following code snippet if the linked list contains nodes with values 3 -> 7 -> 10 -> NULL and we search for 7?
typedef struct Node {
  int data;
  struct Node* next;
} Node;

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

int main() {
  Node n3 = {10, NULL};
  Node n2 = {7, &n3};
  Node n1 = {3, &n2};
  int found = search(&n1, 7);
  printf("%d", found);
  return 0;
}
medium
A. Compilation error
B. 0
C. 7
D. 1

Solution

  1. Step 1: Trace the search function

    The function checks each node's data: 3 (no), 7 (yes), so it returns 1 immediately.
  2. Step 2: Understand main output

    Main prints the returned value, which is 1, indicating value found.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    Value found = 1 [OK]
Hint: Returns 1 if found, else 0 [OK]
Common Mistakes:
  • Expecting the value itself to be printed
  • Confusing return 1 with return 0
  • Thinking code causes compilation error
4. Identify the error in the following code that searches for a value in a linked list:
int search(Node* head, int value) {
  Node* current = head;
  while(current->next != NULL) {
    if(current->data == value) {
      return 1;
    }
    current = current->next;
  }
  return 0;
}
medium
A. The function returns 1 even if value is not found
B. The pointer current is not initialized
C. The loop condition misses checking the last node
D. The function should return the node pointer instead of int

Solution

  1. Step 1: Analyze loop condition

    The loop runs while current->next != NULL, so it stops before last node.
  2. Step 2: Consequence of condition

    The last node's data is never checked, so search may miss value at last node.
  3. Final Answer:

    The loop condition misses checking the last node -> Option C
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Loop until current != NULL to check all nodes [OK]
Common Mistakes:
  • Using current->next != NULL instead of current != NULL
  • Assuming last node is checked automatically
  • Confusing return types
5. Given a linked list with nodes containing values 5 -> 8 -> 12 -> 20 -> NULL, write a function to search for a value and return the position (1-based) if found, or -1 if not found. What will be the output when searching for 12?
hard
A. -1
B. 3
C. 2
D. 4

Solution

  1. Step 1: Understand position counting

    Positions start at 1 for head node, increment by 1 for each next node.
  2. Step 2: Locate value 12 in list

    Values: 5 (pos 1), 8 (pos 2), 12 (pos 3), so 12 is at position 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Value 12 is third node [OK]
Hint: Count nodes from head until value found [OK]
Common Mistakes:
  • Returning zero-based index instead of 1-based
  • Returning -1 even if value exists
  • Confusing position with value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard