Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

we can use a DP here
for all numbers from i=1 to n.
Therefore time complexity O(n2)

- DashDash February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

DP for all numbers from i=1 to n doesn't necessarily suggest O(n^**2)

- BingBang February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sort takes nlogn, then sum takes N*L

- zyfo2 February 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(Not sure about the minimum subset in question it looks to be a minimum of 2)
Rest part is simple

// changing the functionality of pair
static int ways_sum =o;
static int min_counter =0;

void Pair(int a[], int size_array,int sum)
{
Loop ( i=0;a[] <size_array; i++)
{
Loop(j=1;j<array-size;j++)
{
Loop(k=j;k<array_size;k++)
{
ways_sum =a[i] + a[k];
if ( ways_sum == SUM)
{
min_counter++;
break;
}
}
}

}

- sanchayana2007 February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope.
counterexample:
L=100
2 97 1

correct answer is 3
your algorithm doesn't even give the worse answer 4 (97, 1, 1, 1) which would be computed with only 2 numbers.
you can use a number multiple times!!

- BingBang February 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is nothing but variation of subsetsum problem which can be solved using dynamic programming:

geeksforgeeks.org/dynamic-programming-subset-sum-problem/

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

Subset Sum Problem. DP is the best approach and uses n2 run time

- hprem991 February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not seeing how DP helps.

int ways(int [] array, int target, int start, int setSize)
	{
		int count = 0;
		if (setSize == 1)
		{
			for (int i=start; i<array.length; ++i)
			{
				if (target == array[i])
				{
					++count;
				}
			}
		}
		else
		{
			for (int i=start; i<array.length; ++i)
			{
				if (target >= array[i])
				{
					count += ways(array, target - array[i], i, setSize - 1);
				}
			}
		}
		return count;
	}
	
	int minimumSubset(int [] array, int target)
	{
		System.out.println("Target = " + target + ", array = " + Arrays.toString(array));
		int s;
		int count = 0;
		for (s=1; s<=array.length; ++s)
		{
			count = ways(array, target, 0, s);
			if (count > 0)
			{
				break;
			}
		}
		System.out.println("s = " + s + ", ways = " + count);
		return 0;
	}

- rbrandonstewart February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you need memoization otherwise I think you recompute the same subproblems multiple times

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

It is a DP problem, modified version of coin change problem..

- sjain February 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try this.sort the array first and then go from last .check whether last element is smaller than number.If yes then get the remainder.Have a binary search of it and then take the largest of all numbers which are smaller than the remainder.continue this.Please correct me if I am wrong.

- mani 4m sklm March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

greedy does not work

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

It is a np complete subset problem and hence can be done by backtracking

- mani 4m sklm March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope, it's Dynamic Programming actually. Read the other comments.

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

in the given question if i take example number 3
the output should be 2 2 ({7,7} and {1,1,1,1,1,1,1}) as there is no restriction in how many times we can use the given number ?? please comment if you think i am wrong

- gOOfy June 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it asks for the minimum number of numbers

- BingBang June 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

sry i interpretate wrong logic.

- atulsakhala February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

so wrong

- BingBang February 10, 2013 | Flag


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