Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Implementing Linear Search
00:00
5 left

Implementing Linear Search

EasyPython

Problem

A Swiss Re risk-processing component receives record identifiers in an unsorted list. Implement a linear search that returns the index of the first occurrence of a target identifier, or -1 if the identifier is not present.

Formal Specification

Write a function that accepts:

  • records, a list of integers representing record identifiers.
  • target, an integer identifier to find.

Return an integer index. The index must be the position of the first element in records equal to target. Return -1 when no element matches. The list is not sorted, so the function must inspect elements from left to right.

Constraints

  • 0 <= len(records) <= 10^5
  • -10^9 <= records[i], target <= 10^9
  • The input list is unsorted
  • Do not modify records

Function Signature

def linear_search(records, target):
Interviewer

Your question is Implementing Linear Search. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.