Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Pair Sum to Target

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

Your question is Pair Sum 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

Deloitte's consulting analytics tools may need to quickly validate whether two observed integer values satisfy a target relationship. Given an integer array nums and an integer x, determine whether two distinct elements sum exactly to x.

Return True if such a pair exists. Return False otherwise. The same array element cannot be used twice, but duplicate values at different indices may form a valid pair.

Formal Specification

Implement has_pair_with_sum(nums, x).

  • Input: nums, a list of integers, and x, an integer target.
  • Output: A boolean indicating whether two distinct indices i and j exist such that nums[i] + nums[j] == x.

Constraints

  • 0 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= x <= 10^9
  • Values may be duplicated
  • The input array may be unsorted

Function Signature

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