This is a Google interview question, see here for a youtube link.
The task:
Find 2 integers from an unordered list that sum to a given integer.
- Given an unordered list of integers, find 2 integers that sum to a given value, print these 2 integers, and indicate success (exit 0). They don't need to be any particular numbers (i.e. the first 2 integers summing to the right number), any pair that sums to the value will work.
- an integer is positive and greater than zero.
- a list of integers can be in any data structure including a file of integers - one integer per line.
- if no integers can be found, indicate a failure (exit 1).
- two integers at different positions in list must be returned. (i.e. you can't return the same number from the same position twice)
(Note: in the video, these are not exactly the requirements. The 'interviewer' changed his multiple times.)
eg.
sum2 8 <<EOF
1
7
4
6
5
3
8
2
EOF
Prints 3
and 5
and exit status is 0.
Note that in this 1,7
and 2,6
would also be allowed results.
sum2 8 <<EOF
1
2
3
4
Returns exit status 1 since no possible combo. 4,4
isn't allowed, per rule 5.