Target-Sum Pair with Two Pointers
Updated
Problem statement
Return true if two values at distinct array positions sum to target; otherwise return false. Input order is arbitrary and repeated values are allowed. An array with fewer than two entries returns false.
For the final approach, sort a copy and use two pointers moving inward from the smallest and largest values. The scan takes linear time after sorting.
Function
hasTargetSumPair(nums: int[], target: int) → booleanExamples
Example 1
nums = [3,1,4,2]target = 6return = trueThe values 4 and 2 occur at distinct positions and sum to 6.
Example 2
nums = [3,1]target = 6return = falseThe single occurrence of 3 cannot be reused.
Example 3
nums = [-2,0,1,3]target = 1return = trueA negative value and a positive value can form a valid pair.
Constraints
0 <= nums.length <= 5000.-1000000000 <= nums[i], target <= 1000000000.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Assign CookiesAmazon · PHONE SCREEN
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Squares of a Sorted ArrayStryker · PHONE SCREEN
- Squares of a Sorted ArrayGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Merge Into the Zero-Padded ArrayAdobe · PHONE SCREENAdobe interview questions
- Sorted Squares in Descending OrderWhatNot · PHONE SCREENWhatNot interview questions
- Assign CookiesExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Merge Two Sorted ArraysZillow · PHONE SCREENZillow interview questions
- Merge Sorted Arrays with DuplicatesMygate · PHONE SCREENMygate interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)