## Infosys Interview Question for Software Engineer / Developers

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

``````import java.util.ArrayList;
import java.util.List;

/**
*
* Find all the subset of an array, having integers from 1 to 9, such that sum of each subset is 10.
*
*/
public class SubsetSum {

public SubsetSum(int[] arr, int sum) {
super();
this.arr = arr;
this.baseSum = sum;
}

public List<List<Integer>> find(){

List<List<Integer>> allSubsets = new ArrayList<List<Integer>>();

for( int i =0; i < arr.length; i++ ){

int value = arr[i];

if( value > 0 && value < 10 ){

List<List<Integer>> subsetTails = getSubsets( baseSum - value, i+1 );

for(List<Integer> tail : subsetTails ){

List<Integer> singleSubset = new ArrayList<Integer>();

if( isBaseSumSatisfied( singleSubset ) ){
}
}
}
}

return allSubsets;
}

//==== PRIVATE =====

private final int[] arr;
private final int baseSum;

private List<List<Integer>> getSubsets( int sum, int offset ){

if( sum <= 0 || offset >= arr.length){
return new ArrayList<List<Integer>>();
}

List<List<Integer>> subsets = new ArrayList<List<Integer>>();

for( int i = offset; i < arr.length; i++ ){

if( arr[i] > 0 && arr[i] < 10 ){

List<List<Integer>> posibleTails = getSubsets(sum - arr[i], i+1);

if( posibleTails.isEmpty() ){
List<Integer> singleSubset = new ArrayList<Integer>();
}
else {
for( List<Integer> tail : posibleTails ){
List<Integer> singleSubset = new ArrayList<Integer>();

}
}
}
}
return subsets;
}

private boolean isBaseSumSatisfied(List<Integer> singleSubset ){

int actualSum = 0;

for(int value : singleSubset ){
actualSum += value;
}
return baseSum == actualSum;
}

}``````

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

output:

arr: {11,16,11,9,7,13,5,3,1,14}
[9, 1], sum: 10
[7, 3], sum: 10

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

dynamic programming problem...same as coin change

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

Correct its DP & Variation of Coin Change

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

1. Sort the given array by ascending or descending mode by using the quick sort algorithm
2. Subtract one by one every element of the given array from the given number (10) and using the binary search algorithm you will find the next pair.
The complexity of this algorithm is O(LogN)

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

1. Sorting is O(nlogn). Quicksort even can do this in O(n^2).
2. No one said its only pairs! You can have 7+3+1 or 4+3+2+1.
3. Complexity is O(nlong) and not O(logn) because you do O(logn) for n elements.

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

You are also Correct but these are called group so requires backtracking with DP isn't it.

Shashank

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

Hey, did u apply for Infosys in India or US? Is there any other way to apply to Infosys, apart from their online application? because I don't think their online system is working that well. There is absolutely no response for any application, not even application receipt or acknowledgement. If there is, please do let me know.
Thanks!

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

can anybody give solution as a recursive function of c/c++?

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

This is a dynamic programming problem. Given an array A of numbers:
and the recurrence:

``````T(i) = Set of sets T(i-1), {A[i]}, {{T(j)} union A[i]} for all j < i s.t. sum in the sets + A[i] <= 10
if A(i) <= 10

T(i-1)     if A(i) > 10``````

Consider the array and the memoization as follows:

``````A   Set of sets < 10

17  {Empty}
6   {6}
9   {6}, {9}
2   {6}, {9}, {2}, {2,6}, {2,9}
7   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}
8   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}
1   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
3   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}
4   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}, {4}, {4, 6}, {4, 2}, {1, 4}, {1, 2, 4}, {3, 4},
{3, 2, 4}, {3, 1, 4}
7   {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}, {4}, {4, 6}, {4, 2}, {1, 4}, {1, 2, 4}, {3, 4},
{3,2,4}, {3,1,4}, {7}, {7,2}, {1,7}, {1,2,7}, {3,7}``````

Now look at the sets of sets and select the ones that sum up to 10! But this algo is O(n^2).

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

BTW I implied keeping a single list of sets above and not each set of set for each index. Just clarifying :)

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

``````public class SubsetSum {

/**
* @param args
*/
public static void main(String[] args) {
int num[] = new int[]{11,16,11,9,7,13,5,3,1,14};
count(num,10);

}

static void count(int num[],int sum){

for(int i=0;i<Math.pow(2, num.length);i++){
int set[]= new int[num.length];
int tsum=0;
for(int j=0;j<num.length;j++){
if((i&(1<<j))!= 0){
set[j] = 1;
tsum+=num[j];
}
}
if(tsum ==  sum){
for(int k=0;k<num.length;k++){
if(set[k] == 1)System.out.print(" "+num[k]);
}
System.out.print(" : "+sum+" \n");
}
}

}

}``````

using bit operation we can get all the subsets but its limited by the input size :)
Actually the subset sum problem is a NP complete problem.

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.