Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Pair Summing to Target

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

Your question is Find Pair Summing to 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

At Stripe, you are given an unsorted list of integers and a target value. Return the indices of the two distinct elements whose values add up to the target.

Formal Specification

Implement a function that takes:

  • nums: a list of integers
  • target: an integer

Return:

  • A list of two integers [i, j] where i != j and nums[i] + nums[j] == target
  • If no valid pair exists, return []

You may assume indices can be returned in any order unless otherwise specified.

Constraints

  • 2 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Use two distinct indices
  • Return [] if no valid pair exists

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