DIY: Two Sum

Solve the interview question "Two Sum" in this lesson.

Problem statement

In this challenge, you are given a list of integers called numbers and an integer called target. You are required to find the indices of two numbers that add up to the target value.

Note:

  • You cannot use the same element twice in the output. For example, if the value target/2 is present at the index i, then [i, i] is not a valid solution.
  • Also, you can assume that each list has only one solution.
  • The order of elements in the output does not matter.

Input

The function’s first input will be a list of integers called numbers and the second input will be an integer called target. The following is an example input:

[83, 97, 25]
108

Output

The function’s output will be a list of two integers representing the indices of the elements that sum to the target value. The following is the output for the inputs given above:

[0, 2]

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.