Interview Question
Share Lesson

Target Two Sum

Given an array of unsorted unique integers, write a function getTargetIndexes that returns an array of arrays of the index pairs for each of the two numbers that add up to the target.

  • Prioritize run time complexity over memory
  • An item can not match with itself
  • The same pair of indexes should only be included once
  • The order of the returned array and its items does not matter
 

Breakdown

Loading...
Follow teacher
Share:

Table of Contents

Validate My Answer

  1. You can do this brute force, but it will be the worse solution at O(n2) time.

  2. Keep the constraints in mind. You don't have to worry about duplicate values in the array.

  3. By adding another data structure, you can do this in O(n) time and O(n) space.

  4. Remember you can't add a number to itself (ie. the solution is the same index twice). However, you can add a number to zero. For example, array = [4,8,0], target = 8: You can't say [0,0] for 4+4, but you can say [1,2] for 8+0.

  5. Does your solution handle negative numbers?

  6. Does your solution handle an empty array input?

  7. Make sure your solution doesn't include repeated indexes. For example, if you already have [1,4] in your results, you shouldn't also have [4,1]. However, having either one of those is acceptable based on the constraints.

Loading...
Follow teacher
Share:

Table of Contents

Test Results

Run your code and see results here...

/**
 * @param {List[int]} array
 * @param {int} target
 * @return {List[List[int]]}
 */
const getTargetIndexes = (array, target) => {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access

Target Two Sum

Given an array of unsorted unique integers, write a function getTargetIndexes that returns an array of arrays of the index pairs for each of the two numbers that add up to the target.

  • Prioritize run time complexity over memory
  • An item can not match with itself
  • The same pair of indexes should only be included once
  • The order of the returned array and its items does not matter
 
/**
 * @param {List[int]} array
 * @param {int} target
 * @return {List[List[int]]}
 */
const getTargetIndexes = (array, target) => {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access