DSA SectionEasy

Linear Search

Detailed guide and Python implementation for the 'Linear Search' problem.

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?
Consider using Searching-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.