Back to Practice Dashboard
DSA SectionEasy
Linear Search
Learn how to solve the 'Linear Search' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function linear_search(arr, target) that takes a list of integers arr and an integer target. It scans the list from start to finish and returns the 0-based index of the first occurrence of target. If target is not present in the list, return -1.
Constraints
- •0 <= len(arr) <= 1000
- •-10^9 <= arr[i], target <= 10^9
Examples
Example 1
Input
arr = [4, 2, 9, 1, 7], target = 9
Output
2
Explanation
The target 9 is found at index 2 of the list [4, 2, 9, 1, 7].
Example 2
Input
arr = [4, 2, 9, 1, 7], target = 5
Output
-1
Explanation
The target 5 is not present in the list, so we return -1.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.