0
0
DSA C++programming~20 mins

Linear Search Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Linear Search on Array
What is the output of the following C++ code that performs a linear search for the value 7 in the array?
DSA C++
int arr[] = {3, 5, 7, 9, 11};
int n = 5;
int target = 7;
int index = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] == target) {
        index = i;
        break;
    }
}
std::cout << index << std::endl;
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
Remember that array indexing in C++ starts at 0.
Predict Output
intermediate
2:00remaining
Linear Search Result When Target Not Found
What will be the output of this C++ code when searching for 10 in the array?
DSA C++
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int target = 10;
int index = -1;
for (int i = 0; i < n; i++) {
    if (arr[i] == target) {
        index = i;
        break;
    }
}
std::cout << index << std::endl;
A-1
B4
C0
D5
Attempts:
2 left
💡 Hint
If the target is not found, the index remains -1.
🔧 Debug
advanced
2:00remaining
Find the Bug in Linear Search Implementation
This code is supposed to perform a linear search but does not work correctly. What is the output and why?
DSA C++
int arr[] = {2, 4, 6, 8};
int n = 4;
int target = 10;
int index = -1;
for (int i = 1; i <= n; i++) {
    if (arr[i] == target) {
        index = i;
        break;
    }
}
std::cout << index << std::endl;
A2
B-1
C3
DRuntime error
Attempts:
2 left
💡 Hint
Check the loop boundaries and array indexing carefully.
🧠 Conceptual
advanced
1:00remaining
Time Complexity of Linear Search
What is the worst-case time complexity of linear search on an array of size n?
AO(n)
BO(log n)
CO(n log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider how many elements you might check if the target is not present.
🚀 Application
expert
3:00remaining
Linear Search on Linked List
Given a singly linked list with nodes containing values [10, 20, 30, 40], what is the output of searching for 30 using linear search?
DSA C++
struct Node {
    int data;
    Node* next;
};

Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};
head->next->next->next = new Node{40, nullptr};

int target = 30;
int index = 0;
Node* current = head;
while (current != nullptr) {
    if (current->data == target) {
        break;
    }
    current = current->next;
    index++;
}
if (current == nullptr) index = -1;
std::cout << index << std::endl;
A1
B3
C2
D-1
Attempts:
2 left
💡 Hint
Count nodes starting from 0 until you find the target value.