Interview Questions

Given a sorted array and a number ......

Microsoft Interview Questions and Answers


(Continued from previous question...)

137. Given a sorted array and a number ......

Question:
Given a sorted array and a number, find two numbers which sum to the number. Write test cases for same.


maybe an answer:


Use two indexes, one at the beginning and one at the end.

while(start < end) {
if (a[start] + a[end] > number)
end--;
else
if(a[start] + a[end] < number)
start++;
else
break; // found
}

(Continued on next question...)

Other Interview Questions