Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

subset sum problem.Google it up.

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

+1, even if this problem talks about enumerating all sets. Sethi's divide and conquer algorithm for subset sum can be used here, though.

In an interview, I believe all the interviewer want to see is if can write a simple recursive method...

- Loler June 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Loler, I've searched for "Sethi's divide and conquer algorithm", but I couldn't find anything. Typo?

- oOZz June 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@oOZz: Yes, that is a typo. It is not Sethi, but Sahni. The wiki page mentions it.

- Loler June 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene: Right.

- Loler. June 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an NP-Hard Problem
stackoverflow.com/questions/12837431/find-combinations-sum-of-elements-in-array-whose-sum-equal-to-a-given-number

- imrhk June 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void SubSets(int s, int k ,int r)
{
	
	if( s + m_arr[k] == target )
	{
		choose[k] = 1; 
		for(int j= 0; j < 5 ; j++)
		{
			if( choose[j] == 1)
			{
			   cout<<"  ";
			   cout<<m_arr[j];
			}		
			
		}
		for(int j= 0; j < 5 ; j++)
		{
			  choose[j] = 0;
		}
		cout<<endl;
	}
	else if ( (s + m_arr[k] + m_arr[k+1]) <= target)
	{
       choose[k] = 1; 
	   SubSets(s+m_arr[k] , k+1, r-m_arr[k]);
	   
	}
	if(((s + r + -m_arr[k]) >= target) && ( (s + m_arr[k+1]) <= target))
	{
		choose[k] = 0;
		SubSets(s ,k+1, r-m_arr[k]);
		
	}
 
}

- Anonymous June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class FindSquare {
	
	public boolean isSquare(int value)
	{
		int sum = 0;
		int odd = 1;
		boolean result = false;
		while(value > sum)
		{
			sum = sum + odd;
			odd = odd + 2;
		}
		
		System.out.println(sum);
		if(sum == value)
		{
			result = true;
		}
		
		return result;
	}
	
	public static void main(String[] args)
	{
		System.out.println(new FindSquare().isSquare(16));
	}
}

- Himadri, Toronto June 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry posted in wrong question, apology

- Himadri, Toronto June 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You can solve it like:
a.First sort the array in non-decreasing order.Take two pointers left and right.Left point to the start and the right pointer points to the end of the array.
b.while(left<right)
b.check whether a[left]+a[right]<k.If yes then increment the left pointer by one.
c.check whether a[left]+a[right]>k.If yes decrement the right pointer by one.
d.If a[left]+a[right]==k.Return true.

- vgeek June 13, 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