Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two-Sum Array Function

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

Your question is Two-Sum Array Function. 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

Within an Auto-Owners Insurance claims review workflow, identify two transaction values whose combined amount matches a specified target. Given an unsorted array of integers, return the indices of the two distinct elements whose values add up to the target.

You may assume that each valid input contains exactly one solution. Return the indices in the order they are discovered, and do not use the same array element twice. If the input violates the guarantee, return an empty list.

Formal Specification

Implement two_sum(nums, target).

  • Input: nums, an array of integers, and target, an integer.
  • Output: An array containing two zero-based indices [i, j] such that nums[i] + nums[j] == target and i != j.
  • Invalid or unsatisfied input: Return [] if no qualifying pair exists.

Use a solution that runs in linear time on average. A brute-force pair search is correct but does not meet the preferred efficiency for larger claim batches.

Constraints

  • 2 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Each valid test case has exactly one solution
  • The same element cannot be used twice

Function Signature

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