Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Index Sum Target

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

Your question is Array Index Sum Target. 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

John Deere Operations Center receives integer telemetry readings from connected equipment. Given an array of readings and a target sum, return the indices of the first pair whose values add up to the target.

Scan the array from left to right. A pair is considered the first finding when its second index is encountered first; if multiple earlier values can pair with that element, use the smallest first index. Return the pair of indices in ascending order. Do not reuse the same array element. If no pair exists, return an empty list.

Formal Specification

Implement find_first_pair(nums, target), where nums is a list of integers and target is an integer. Return a two-element list [i, j] such that i < j and nums[i] + nums[j] == target, or return [] when no valid pair exists.

Constraints

  • 0 <= nums.length <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Each element can be used at most once
  • Return the first pair found during a left-to-right scan

Function Signature

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