Facebook Interview Question for Software Engineer / Developers


Team: Facebook groups
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;
}

- jason April 11, 2018 | Flag Reply
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;
	}

- aifra2000 April 12, 2018 | Flag Reply
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;
			}
		}

- Venkatesh April 19, 2018 | Flag Reply
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.

- Asif Garhi April 20, 2018 | Flag Reply
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;
                    }
                    complimentsWhenKey.add(due - a[i]);
                }
            }
            k++;
        }
        return false;

}

- jayakrishnan.somasekharannair April 29, 2018 | Flag Reply
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

- Andrea.Ferrando May 17, 2018 | Flag Reply
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);
            }
        }
    } 
}

- Skander J May 29, 2018 | 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