Bloomberg L.P. Interview Question: Given a sum, find two numbers... | Glassdoor

Interview Question

Software Engineer Interview

Given a sum, find two numbers in an array with that sum.

Answer

Interview Answer

2 Answers

0

Using JS:

nums = [2,13,61,512,3,6,7,88,17]

sum = 15

for(var i = 0; i -1) {
    return nums[match];
  }
}

Andy on Jun 17, 2015
0

The other answer works, but is not efficient (It is O(n^2))

To make it O(n), simply make a hashmap. For every number, check to see if the number exists in the hashmap, otherwise store [sum-currentNumber]

sidd on Aug 2, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.