0
0
DSA Javascriptprogramming~20 mins

Linear Search Algorithm in DSA Javascript - 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 for Existing Element
What is the output of the following JavaScript code that performs a linear search for the number 7 in the array?
DSA Javascript
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }
  return -1;
}

const numbers = [3, 5, 7, 9, 11];
console.log(linearSearch(numbers, 7));
A1
B3
C-1
D2
Attempts:
2 left
💡 Hint
Remember that array indexes start at 0 in JavaScript.
Predict Output
intermediate
2:00remaining
Output When Element is Not Found
What does the following code print when searching for 10 in the array?
DSA Javascript
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }
  return -1;
}

const numbers = [1, 2, 3, 4, 5];
console.log(linearSearch(numbers, 10));
A-1
B5
C0
Dundefined
Attempts:
2 left
💡 Hint
If the target is not in the array, what does the function return?
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Linear Search
What is the worst-case time complexity of the linear search algorithm when searching in an array of size n?
AO(n log n)
BO(n)
CO(1)
DO(log n)
Attempts:
2 left
💡 Hint
Think about how many elements the algorithm might check in the worst case.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Linear Search Implementation
What is the output of the following code when searching for 4 in the array?
DSA Javascript
function linearSearch(arr, target) {
  for (let i = 0; i <= arr.length; i++) {
    if (arr[i] === target) {
      return i;
    }
  }
  return -1;
}

const numbers = [1, 2, 3, 4, 5];
console.log(linearSearch(numbers, 4));
ATypeError: Cannot read property 'undefined' of undefined
B-1
CReturns 3
DSyntaxError
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🚀 Application
expert
1:30remaining
Number of Comparisons in Linear Search
If you perform a linear search for the target value 8 in the array [2, 4, 6, 8, 10], how many comparisons does the algorithm make before finding the target?
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
Count how many elements the algorithm checks until it finds 8.