SWAPTON SOLUTIONS Interview Question for Java Developers


Team: DEV
Country: India
Interview Type: Written Test




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

This can be done using DP

void FindSet(int *a, int x, int Sum, int n)
{
if(a[n] > Sum)
FindSet(a,x,Sum,n-1);
else if(Sum == 0 && x == 0)
{
//found print and return
}
else
{
FindSet(a, x, Sum, n-1);
FindSet(a, x-1, Sum-a[n], n-1);
}
}

- DashDash March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Recursion in Python

def subsets(low, high, n, target):
    def f(low, n, target, prefix):
        if low >= high:
            return
        if n == 1:
            if low <= target < high:
                yield prefix + [target]
            return
        for i in range(low, high):
            cnt = 0
            for result in f(i+1, n-1, target-i, prefix + [i]):
                yield result
                cnt += 1
            if cnt == 0:
                break

    for result in f(low, n, target, []):
        yield result

for ans in subsets(10, 100000, 4, 55):
    print ans

- showell30@yahoo.com March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can an implementation be shown in C/java/.Net please??

- Anonymous March 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A Knapsack Problem.
Just give all the elements a value 1, and run the Knapsack algorithm using DP.
That should give u the solution.

Hope it helps. Let me know if you need further clarification.

- Bevan March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Running this will give a single set of values. What if all the possible sets are needed??

- Anonymous March 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given that all the elements are exactly 1 unit apart, one could find one combination that works and then generate other combinations by generating all combinations in which one of the set elements is 'moved left' and another is 'moved right', but only for those elements that can be moved (aren't stuck against another element). This algorithm would exclude a lot of combinations that can't work, like choosing from a set of elements in which no numbers add up to the number sought.

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

@Anonyomous, you're basically describing a "successor" function, which is always an interesting way to think about problems involving combinations and permutations, and which leads to "odometer" algorithms. Basically, for all the subsets that can produce 55, you know that they have some sort of natural ordering, and if you're given one of the subsets, then there should be a deterministic way to perturb its elements in a minimal way to get the next result.

In this problem, you've already described the basic mechanism to some degree. Start by trying to perturb the second to last element, but it might be "stuck," as you say. In that case, you have to perturb the third to last element instead. And, of course, for every number you perturb in one direction, perturb another number in the opposite direction.

- showell30@yahoo.com March 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package swaption;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FindSum {

    /**
     * @param args
     */
    public static void main(String[] args) {

	int[] arr = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
	printSubsets(arr, 1, 4, 55, 55, new HashSet<Integer>(), new HashSet<Integer>(), new ArrayList<Set<Integer>>());
    }

    private static void printSubsets(int[] arr, int count, int x, int y, int remainder, Set<Integer> used,
	    Set<Integer> elements, List<Set<Integer>> previouslyFound) {

	for (int i = 0; i < arr.length; i++) {
	    if (used.contains(i)) {
		continue;
	    }
	    int diff = remainder - arr[i];
	    used.add(i);
	    if (count + 1 < x) {
		printSubsets(arr, count + 1, x, y, diff, used, elements, previouslyFound);
	    } else if (count + 1 == x) {
		if (elements.contains(diff) && !isAlreadyFound(used, previouslyFound)) {
		    previouslyFound.add(new HashSet<Integer>(used));
		    System.out.println(getElementsFromArray(arr, used, diff) + " = " + y);
		}
	    }
	    if (count == 1) {
		elements.add(arr[i]);
	    }
	    used.remove(i);
	}
    }

    private static boolean isAlreadyFound(Set<Integer> newSet, List<Set<Integer>> previouslyFound) {
	
	for(Set<Integer> set : previouslyFound) {
	    if(set.containsAll(newSet)) {
		return true;
	    }
	}
	return false;
    }
    
    private static String getElementsFromArray(int[] arr, Set<Integer> indexes, int diff) {

	StringBuilder sb = new StringBuilder();
	for (Integer i : indexes) {
	    sb.append(arr[i] + " + ");
	}
	sb.append(diff);
	return sb.toString();
    }
}

- Anonymous March 24, 2013 | 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