FastPrepTwo Sum
Problems/Two Sum

Two Sum

AMD logoAMD● EasyINTERNFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names Two Sum in Python. The deterministic tie rule, no-solution value, bounds and portable interface are authored for practice; approximate source match 85%.

Return two distinct zero-based indices [i,j] such that i < j and nums[i] + nums[j] == target.

If several pairs work, choose the smallest first index, then the smallest second index. If none work, return [-1,-1].

Function

twoSum(nums: int[], target: int) → int[]

Examples

Example 1

nums = [2,7,11,15]target = 9return = [0,1]

Indices 0 and 1 contain 2 and 7, whose sum is 9.

Example 2

nums = [1,4,2,3]target = 5return = [0,1]

Both [0,1] and [2,3] work. The smaller first index selects [0,1].

Constraints

  • 0 <= nums.length <= 1000.
  • -1000000 <= nums[i] <= 1000000.
  • -2000000 <= target <= 2000000.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int[] twoSum(int[] nums, int target) {
    // Write your code here.
}
Checking account…