Facebook Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 vote

Use a hash map.
Suppose array is arr = {2,8,3,6,4,7,9}
Now use a hash map
key = arr[i]
value = sum - arr[i]

if(value>0){
   map.put(key,value);
   if(map.containsKey(value))
      print key and value that add to the sum
}

The idea here is that suppose we want sum 5
now map will be
2,3
No entry for 8 as value >0
3,2 -> Here the value i.e. 2 exists as key -> 2,3 hence we have the sum 5

- Gamodg October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Correction
No entry for 8 as value <0

- gamodg October 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should consider case that array contains negative values .

- Aditya October 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

negative will be the same, if the input array allows it. just put all the value to the hashmap.

- sophia August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for your example there is an exception (mentioned in the hint) - sum 4 and your code will return true. Hinted case - case when element = half of sum.

- anvol April 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Try with Hash table.
Put all the array elements in Hash table.
Search for first array element in hash table and then x-a[0] value in hash table.
If it exists, return true.
else do this for other array elements present in hash table.

Complexity - O(n)

- Richa Aggarwal October 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good One

- Vivekananda April 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

It can be implemented w/o any additional DS's in O(n Log n) time and with O(1) storage.
The below code and the driver app will find all the valid pairs, also included are the inlined comments:

#include <stdio.h>
#include <algorithm>
/*
Given an array of integers in the range INT_MIN to INT_MAX,
find all the pairs of elements that sum up to the given number @sum
Algorithm:
- Sort the input array
- Keep summing up the elements at the lowest and the highest current indices
- If the sum is > than the result, decrement the upper index
- If the sum is < than the result, increment the lower index
- If the sum = result, then
- found a valid pair; print the numbers
- converge the lower and upper indices (++lower, --upper)
- Loop until both indices meet
The above algorithm will find all the valid pairs, including positive,
negative and 0.
*/
bool find_sum(int arr[], int cnt, int sum)
{
int l, u, k;
bool bret = false;

/* Sort the input array */
std::sort(&arr[0], &arr[cnt]);
u = cnt - 1;
l = 0;

while (l < u) {
k = arr[l] + arr[u];
if (k == sum) { // found the two elements
printf("SUM: %-3d ELEMENTS (%d %d): %d %d\n",
sum, l, u,
arr[l], arr[u]);
bret = true;
l++;
u--;
}
else if (k < sum)
++l;
else
--u;
}
return bret;
}
int main(int argc, char* argv[])
{
int arr[] = {7, -4, 1, 0, -2, 3, 8, -9, 10, 1, 0, 20, 2};
int cnt = sizeof(arr)/sizeof(arr[0]);

for (int i = -10; i < 20; i++)
find_sum(arr, cnt, i);
return 0;

}
}}}

- ashot madatyan May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public boolean sumExists ( int [ ] array, int sum ) {

	if ( array == null )
		return false;

	HashSet<Integer> hs = new HashSet<Integer> ( );

	for ( int i=0; i<array.length; i++) {
		if ( hs.contains(sum-array[i] )
			return true;
		hs.add(array[i]);
	}
	
	return false;
}

- Zero2 November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash the array.

Iterate the array and check if x-a[i] exists in the hash.
Corner case is if x=4 and a[i]=2 and no other two is there in the array.

- sethuraj.32 October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Gamodg try with input {1,3,5,7,9}
sum=10.

- Anonymous October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

only consider existing,not for all answers...true/false

- ttmm January 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create empty set. Iterate from left to right in the array. At each arr[i] item check whether X-arr[i] is already in the set. If yes, return true, else put arr[i] into the set and continue.

Linear time and space, no need to handle special case.

- Safi December 18, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More