Linear Search Algorithm in DSA Javascript - Time & Space Complexity
We want to understand how the time taken by linear search changes as the list size grows.
How many steps does it take to find an item or decide it's not there?
Analyze the time complexity of the following code snippet.
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
This code looks through each item in the list one by one to find the target value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the array one by one.
- How many times: Up to n times, where n is the number of items in the array.
As the list gets bigger, the number of checks grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The steps grow directly with the number of items.
Time Complexity: O(n)
This means the time to find the item grows in a straight line with the list size.
[X] Wrong: "Linear search always takes the same time no matter the list size."
[OK] Correct: The search time depends on where the item is or if it is missing, so bigger lists usually take longer.
Understanding linear search time helps you explain why sorting or other methods can speed up searching in real tasks.
"What if the array was sorted and we used binary search instead? How would the time complexity change?"
Practice
Linear Search Algorithm?Solution
Step 1: Understand linear search process
Linear search checks each element in order, from start to end.Step 2: Compare with other search methods
Binary search divides list, hash uses extra memory, linear is simple sequential check.Final Answer:
Check each element one by one until the target is found or list ends -> Option AQuick Check:
Linear search = sequential check [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming linear search uses extra memory
arr?Solution
Step 1: Understand array indexing in JavaScript
Arrays start at index 0 and go up to length - 1.Step 2: Choose loop condition to avoid out-of-bound error
Loop must run while i is less than arr.length, not equal or greater.Final Answer:
for (let i = 0; i < arr.length; i++) -> Option BQuick Check:
Loop runs from 0 to length-1 [OK]
- Using <= instead of < causing out-of-bound errors
- Starting loop from 1 instead of 0
- Confusing array length with last index
const arr = [4, 8, 15, 16, 23, 42];
function linearSearch(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return -1;
}
console.log(linearSearch(arr, 15));Solution
Step 1: Trace the loop to find target 15
Indices: 0=4, 1=8, 2=15. At i=2, array[i] === 15 is true.Step 2: Return index when target found
Function returns 2 immediately when target matches.Final Answer:
2 -> Option AQuick Check:
Index of 15 in array = 2 [OK]
- Returning value instead of index
- Returning -1 even if target exists
- Off-by-one errors in index
function linearSearch(arr, target) {
for (let i = 0; i <= arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}Solution
Step 1: Identify loop condition error
Loop runs while i <= arr.length, which causes out-of-bound access at i = arr.length.Step 2: Fix loop condition to prevent error
Change condition to i < arr.length to stay within valid indices.Final Answer:
Change loop condition to i < arr.length -> Option CQuick Check:
Loop must stay within array bounds [OK]
- Using <= instead of < in loop condition
- Thinking == vs === causes runtime error here
- Adding break after return (unnecessary)
[3, 5, 7, 7, 9] using linear search. Which code snippet correctly returns the index of the first 7?Solution
Step 1: Understand requirement to find first occurrence
Start from index 0 and return index when first 7 is found.Step 2: Analyze each option for correctness
for (let i = 0; i < arr.length; i++) { if (arr[i] === 7) return i; } return -1; loops forward, returns index correctly. for (let i = arr.length - 1; i >= 0; i--) { if (arr[i] === 7) return i; } return -1; loops backward (last occurrence). for (let i = 1; i <= arr.length; i++) { if (arr[i] === 7) return i; } return -1; has wrong loop bounds. for (let i = 0; i < arr.length; i++) { if (arr[i] == 7) return arr[i]; } return -1; returns value, not index.Final Answer:
for (let i = 0; i < arr.length; i++) { if (arr[i] === 7) return i; } return -1; -> Option DQuick Check:
Forward loop returns first match index [OK]
- Looping backward returns last occurrence, not first
- Returning value instead of index
- Using wrong loop bounds causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
