Skip to main content
Practice

Coding Quiz - Finding Indices for a Target Sum

In this coding quiz, you will write a function to find the indices of two numbers in a given integer array that add up to a specific target.

The user will input an integer array and a target value, and you need to return a list of the indices of the two numbers that sum up to the target value in the array.

For example, if the integer array is [2, 7, 11, 15] and the target value is 9, you should return [0, 1] because the 0th element (2) and the 1st element (7) add up to 9.

The returned list of indices should be sorted in ascending order, and if no such pair is found, an empty list should be returned.


Code Solution
def solution(numbers, target):
# Write your code here
return



Constraints

  • The input array must contain at least two integers.

  • Exactly one solution exists, and you may not use the same element twice.




Example Input/Output

  • Input: Array [2, 7, 11, 15], Target value 9

  • Output: [0, 1]


  • Input: Array [3, 2, 4], Target value 6

  • Output: [1, 2]

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.