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


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