## Facebook Interview Question for Software Engineer / Developers

• 0

Country: United States

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

Java
Time: O(n^2)
Space: O(n)

``````boolean sumOfThree(int[] array, int targetSum) {
Arrays.sort(array);
for (int i = 0; i < array.length-2; i++) {
int left = i+1;
int right = array.length-1;
while (left < right) {
int tripletSum = array[i] + array[right] + array[left];
if (tripletSum == targetSum) {
return true;
} else if (tripletSum < targetSum) {
++left;
} else {
--right;
}
}
}
return false;
}``````

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

``````public static boolean findTriplet(int[] arr,int sum) {
int l,r;

//sum=9;
Arrays.sort(arr);//2,3,4,6
for(int i=0;i<arr.length-2;i++) {
l=i+1;
r=arr.length-1;

while(l<r) {
if(arr[i]+arr[l]+arr[r]==sum)
return true;
else if(arr[i]+arr[l]+arr[r] < sum)
l++;
else
r--;

}

}

return false;
}``````

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

``````for (int i = 0; i < numArray.length - 2; i++) {
for (int j = i; j < numArray.length - 2; j++) {
int tripleSum = a[i] + a[j + 1] + a[j + 2];
if(tripleSum == c)
return true;
}
}``````

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

Sorry but I fail to understand the question. Can you please explain a couple of 'true' and a couple of 'false' condition. Also failing to understand why 2 loops were needed to solve this. It's possible I haven't got the question right.

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

``````public static boolean findIfAnyTripletsOfArraySumsToResult(int[] a, int result) {

int k=0,due =0;
Set complimentsWhenKey = new HashSet();

while(k<a.length){

complimentsWhenKey.clear();
due=result-a[k];
for(int i=0;i<a.length;i++ ) {
if(i==k ){
i=i+1;
}
if(i<=a.length-1) {
if(complimentsWhenKey.contains(a[i])){
return true;
}
}
}
k++;
}
return false;``````

}

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

It's great to use a hash table and store partial results.
jayakrishnan.somasekharannair solution is nice! But it doesn't need to do:
for(int i=0;i<a.length;i++ )
as every time I only need to check the elements at the right of k, no need to restart from zero. I would say:
for(int i=k;i<a.length;i++ )

That's my solution in swift

``````func constantIsSumOf3Elements(_ array:[Int], c:Int) -> Bool {
var hash = Set<Int>()
for (i, element) in array.enumerated() {
hash.removeAll()
let due = c-element
for j in (i+1)..<array.count {
if hash.contains(array[j]) {
print("\(element) + \(due-array[j]) + \(array[j]) = \(c)")
return true
}
hash.insert(due-array[j])
}
}
return false
}

let _ = constantIsSumOf3Elements([6,5,3,2,1,1,21], c:28)``````

//Output: 6 + 1 + 21 = 28

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

``````void findTriplets2(int[] a, int sum) {

HashMap<Integer, Integer> hm = new HashMap<>();

for (int i = 0; i < a.length; i++) {
hm.put(a[i], a[i]);
}

if (a.length == 3 && a[0] + a[1] + a[2] == sum) {
System.out.println(a[0] + " + " + a[1] + " + " + a[2]);
return;
}

for (int i = 0; i < a.length - 2; i++){
for (int j = i + 1; j < a.length; j++){
int diff = sum - (a[i] + a[j]);
if (hm.get(diff) != null) System.out.println(+ a[i] + " + " + a[j] +  " + " + diff);
}
}
}
}``````

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

``````boolean recursiveSumOf3(int[] arr, int sum, int elements, int i) {
if (elements==0 && sum == 0) {
return true;
}
if (i > arr.length-1) {
return false;
}
return recursiveSumOf3(arr, sum-arr[i], elements-1, ++i) || recursiveSumOf3(arr, sum, elements, ++i);
}``````

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.

### 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.