Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Pair Summing to Target

EasyPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Find Pair Summing to Target. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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