Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Two Sum (Alternative Wording)
00:00
5 left

Two Sum (Alternative Wording)

EasyPython

Problem

Implement a solution for the 2Sum problem.

Define def two_sum(nums, target):, where nums is a list of integers and target is an integer. Return the two distinct zero-based indices whose values sum to target; assume exactly one solution exists, with 2 <= len(nums) <= 10^4 and values between -10^9 and 10^9.

For example, nums = [2, 7, 11, 15], target = 9 returns [0, 1], while nums = [3, 2, 4], target = 6 returns [1, 2]. Aim for better than quadratic time.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid pair exists
  • The same array element cannot be used twice

Function Signature

def two_sum(nums, target):
Interviewer

Your question is Two Sum (Alternative Wording). Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.