Firstly, given this is an interview question, an important aspect is to be asking questions. It's very common in the real world to be given a vague spec or a problem that has not been fully considered. If you don't ask, ensure that your program can handle such edge-cases. Be defensive with your code and try to have a bulletproof spec.
Secondly, tests. A great way to blow an interviewer over is to write your tests first, or at least suggest that you would before you start coding. Thinking about tests can help cover the questions that you should be asking anyway and edge-cases you may not have originally thought of, although for the sake of brevity during an interview, they may end up just being a talking point.
As for the problem in question, for the given data set, a naive approach is not going to struggle on a modern computer and if it is genuinely faster for you to code, you could argue that is the fastest solution on that basis. There are some traps to avoid, depending on your approach; a big one depends on interpretation of the problem statement 'return... each pair of integers that add up to 100'. It's not clear if that means that if 0 occurs three times and 100 occurs five times, we should see three pairs of [0, 100] or just a single unique pair. Including all pairs is a more interesting challenge, so I'll assume that from now on until I say otherwise.
The fastest and most scalable algorithm is to build a histogram, that is, to count the number of occurrences of the values (a map or an array can be suitable here). In javascript, using an array, that might look something like this:
var histogram = [101];
for(var i=0; i<histogram.length; i++) { // init
histogram[i] = 0;
}
for(var i=0; i<data.length; i++) { // populate
histogram[data[i]]++;
}
Once you have a histogram, we can observe that number pairs that add up to 100 mirror either side of 50 and, as a result, 50 is a special case. For non-50 numbers, we have as many pairs as the smaller frequency of occurrence. Put into code:
var result = [];
for(var i=0; i<floor(histogram[50]/2); i++) {
result.push([50, 50]);
}
for(var number=0; number<50; number++) {
var inverse = 100 - number;
var pair = [number, inverse];
var numberFrequency = histogram[number];
var inverseFrequency = histogram[inverse];
var pairCount = numberFrequency < inverseFrequency ? numberFrequency : inverseFrequency;
for(var i=0; i<pairCount; i++) {
result.push(pair);
}
}
Obviously this approach needs some adjustment if negative numbers are allowed and would probably want to use a map instead to avoid having a potentially sparse and large array. Overall, it only takes O(n) to insert into the histogram and the other aspects are constant, making this approach optimal.
If we do want to do unique pairs, as per your implementation and the alternative interpretation of the problem statement, we can just change the print loops to if statements. However, there is also an extra end condition: if you have encountered all possible pairs, we no longer need to continue building the histogram and can just print all possible pairs and finish. We can adjust the histogram population code to catch this:
// snip: histogram creation and initialisation
var encountered = -1;
for(var i=0; i<data.length; i++) { // populate
histogram[data[i]]++;
if(data[i] == encountered + 1) {
while(histogram[encountered + 1] > 0) {
encountered++;
}
if(encountered == 100) {
for(var i=0; i<51; i++) {
result.push([i, 100 - i]);
}
break; //end early
}
}
}