This technical interview question requires us to write a function that can identify the indices of two numbers in an array that add up to a target sum specified by the user. Here's one possible implementation:
function findIndices(numbers, targetSum) {
const indices = {
}
;
for (let i = 0;
i < numbers.length;
i++) {
const currentNumber = numbers[i];
const complement = targetSum - currentNumber;
if (indices.hasOwnProperty(complement)) {
return [indices[complement], i];
}
indices[currentNumber] = i;
}
return null;
}
The above function works by iterating over each number in the array and checking if its complementary number exists in the indices object. If it does exist, it returns the indices of the two numbers. If it doesn't exist, it adds the current number to the indices object with its index as the value.
This function has a time complexity of O(n), where n is the number of elements in the array. This is because we need to iterate over each element in the array only once to find the indices of the two numbers that add up to the target sum.