Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Efficient Target Search

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Efficient Target Search. Start with the requirements on the right.

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.

Problem

An AMD ROCm validation utility stores device identifiers in ascending order. Given this sorted array and a target identifier, return the index of the target's first occurrence. Return -1 if the target is absent.

The solution must use binary search rather than scanning the array sequentially, because validation runs may contain millions of sorted entries.

Formal Specification

Implement find_first_device(device_ids, target).

  • Input: device_ids, a list of integers sorted in nondecreasing order, and target, an integer.
  • Output: An integer containing the smallest index i such that device_ids[i] == target, or -1 when no such index exists.
  • The input list must not be modified.

Constraints

  • 0 <= len(device_ids) <= 10^6
  • -10^9 <= device_ids[i] <= 10^9
  • -10^9 <= target <= 10^9
  • device_ids is sorted in nondecreasing order
  • The input list must not be modified

Function Signature

def find_first_device(device_ids, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output