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>();
					singleSubset.add( value );
					singleSubset.addAll( tail );
						
					if( isBaseSumSatisfied( singleSubset ) ){
						allSubsets.add(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>();
					singleSubset.add( arr[i] );					
					subsets.add(singleSubset);
				}
				else {				
					for( List<Integer> tail : posibleTails ){
						List<Integer> singleSubset = new ArrayList<Integer>();
						singleSubset.add( arr[i] );
						singleSubset.addAll(tail);
						
						subsets.add(singleSubset);
					}
				}
			}
		}
		return subsets;
	}


	private boolean isBaseSumSatisfied(List<Integer> singleSubset ){
		
		int actualSum = 0;
		
		for(int value : singleSubset ){
			actualSum += value;
		}
		return baseSum == actualSum;
	}

}

- m@}{ June 07, 2011 | Flag Reply
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

- m@}{ June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dynamic programming problem...same as coin change

- Amm Sokun June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct its DP & Variation of Coin Change

- WgpShashank June 08, 2011 | Flag
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)

- Meruzhan June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous June 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

Shashank

- WgpShashank June 08, 2011 | Flag
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!

- Anonymous June 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- martin June 08, 2011 | Flag Reply
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).

- Ashish Kaila June 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Ashish Kaila June 10, 2011 | Flag
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.

- nmc July 20, 2011 | 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