Home
Refer
Jobs
Alumni
Resume
Notifications

Here's a Technical interview question for Software Engineer, University Graduate, 2025 role at Google: Given an array of integers, write a function to determine if the array contains any two numbers that add up to a specific target sum. Your function should return the indices of the two numbers (i.e., their position in the array). If there are no such numbers, return null.

🚀 Best Answers Get Featured in our LinkedIn Community based on Your Consent, To Increase Your Chances of Getting Interviewed. 🚀

Answer:

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.

References:

© 2024 Referral Solutions, Inc. Incorporated. All rights reserved.