Salesforce Interview Question for Software Engineers


Country: United States




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

What are the assumptions ??
Can there be negative integers ??

- Hari February 10, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Doing brute force with memorization

Tuple<List<int>, List<int>> divideWithMinimunDifference(List<int> a) {
	var min = divide(a, a1, a2, 0, 0, new Dictionary<string, Tuple<int, List<int>, List<int>>())
	return new Tuple<List<int>, List<int>>(min.value1, min.value2);
}

Tuple<int,  List<int>,  List<int>> divide(a, a1, a2, sum1, sum2, Dict<string, Tuple<int, List<int>, List<int>> memo) {
	
	if(a.length == 0)
		return new Tuple<int, List<int>, List<int>(Math.abs(sum1-sum2), new List<int>(a1), new List<int>(a2))

	string hash = getHash(a, a1, a2)
	if(memo.containsKey(hash))
		return memo[hash]

	int min = new Tuple<int, List<int>, List<int>>(int.MinValue, null, null)
	for (int i = 0; i < a.length; i++) {
		ai = a[i]
		a1.append(ai)
		a.removeAt(i)
		cand = divide(a, a1, a2, sum1+ai, sum2)
                if(min.value1 > cand.value1) {
			min = cand
		}

		for (int j = 0; j < a.length; j++) {
			aj = a[j];
			a2.append(aj);
			sum2 += aj;
			a.removeAt(j, ai);
			divide(a, a1, a2, sum1 + ai, sum2 + aj);
			a.insertAt(j);

	                if(min.value1 > cand.value1) {
				min = cand;
			}
		}

		a.insertAt(i, ai);
		
		for (int j = 0; j < a.length; j++) {
			aj = a[j];
			a2.append(aj);
			sum2 += aj;
			a.removeAt(j);
			divide(a, a1, a2, sum1, sum2 + aj);
			a.insertAt(j);
		}
	}

	memo[hash] = min;
	return min;
}

string getHash(List<int> a, List<int> a1, List<int> a2) {
	string a_str = string.join(a, ',');
	string a1_str = string.join(a1, ',');
	string a2_str = string.join(a2, ',');

	string hash = string.Format('{0};{1};{2}', a_str, a1_str, a2_str);
	return hash;
}

- Nelson Perez February 14, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In order for both sums to be as close as possible - each needs to be as close as possible to half of the total sum.
In my approach i first sort the initial set, then go through it from highest to lowest and sum the numbers. If the result of adding a number to the current sum is greater than the half of the total sum - i continue iterating (to the lower numbers) and try to add them

Examples:
{1,2,3,4,90}
s1: 90 (sum 90)
s2: 1,2,3,4 (sum 10)

{1,2,3,14,15,16}
s1: 16,3,2,1 (sum 22)
s2: 15,14 (sum 29)

public static void minSum(List<Integer> set) {
        List<Integer> set1 = new ArrayList<>();
        List<Integer> set2 = new ArrayList<>();

        List<Integer> sortedSet = set.stream().sorted().collect(Collectors.toList()); 

        int totalSum = sortedSet.stream().mapToInt(Integer::valueOf).sum();
        int halfSum = totalSum/2;
        //put highest element n set1
        int maxElement = sortedSet.get(sortedSet.size()-1);
        set1.add(maxElement);

        int s1Sum = maxElement;
        int s2Sum = 0;
        for(int i=sortedSet.size()-2; i>=0; i--) { 
            int curr = sortedSet.get(i);
            if(s1Sum <= halfSum && s1Sum + curr <= halfSum) {
                set1.add(curr);
                s1Sum+=curr;
            } else {
                set2.add(curr);
                s2Sum+=curr;
            }
        }

        System.out.println("S1: sum = " + s1Sum);
        System.out.println("S2: sum = " + s2Sum);

    }

- Poozmak February 23, 2020 | 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