Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Two-Sum Style Array Check

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

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

A Cengage Group learning activity needs to validate whether a learner's selected values contain a pair matching a required total. Given an unsorted array of integers and a target integer, determine whether two distinct array elements add up exactly to the target.

Return True if such a pair exists, and False otherwise. An element may not be used twice, but duplicate values at different indices may form a valid pair.

Formal Specification

Implement has_pair_with_sum(nums, target):

  • Input: nums, a list of integers, and target, an integer.
  • Output: A boolean indicating whether two different indices i and j satisfy nums[i] + nums[j] == target.
  • The input array must not be modified.

Constraints

  • 0 <= len(nums) <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • -2 * 10^9 <= target <= 2 * 10^9
  • The input array must not be modified

Function Signature

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