Seção DSAFácil

Pesquisa Linear

Guia detalhado e implementação de Python para o problema de 'Pesquisa Linear'.

Declaração do problema

Fácil

Escreva uma função linear_search(arr, target) que receba uma lista de inteiros arr e um inteiro target. Ele verifica a lista do início ao fim e retorna o índice baseado em 0 da primeira ocorrência de target. Se target não estiver presente na lista, retorne -1.

Restrições
  • 0 <= len(arr) <= 1000
  • -10^9 <= arr[i], target <= 10^9

Exemplos

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?
Considere usar estruturas de dados específicas de pesquisa, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

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

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.