Interview Question for Software Engineer / Developers


Country: India




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

We can do it in 2 sum problem taking every number subracting with the sum and then finding the 2 numbers from the rest of the array using 2 sum algo(taking hash)

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

If needed you can avoid hashing by sorting the array and using the two pointers trick.

The decision problem (finding if there is at least one triple) is 3SUM hard. Getting a better than quadratic (like DashDash's solution) is an open problem.

Of course, this problem calls for enumerating all, so could go cubic!

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

Yes, i think sorting is the right way.

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

This is my try.........
I think it will work for all combinations not only for 3........
If there is any mistakes, help me to rectify it.........
/*Given a array of numbers and a number N. Find out all combinations of 3 numbers in array whose sum is N.*/
#include<iostream.h>
#include<conio.h>

int n,a[20],N,s,b[20];

void find_comb(int i,int j,int sum)
{
sum+=a[i];
b[j]=a[i];

if((j==N-1)&&(sum==s))
{
cout<<"{";
for(int k=0;k<=j;k++)
cout<<b[k]<<",";
cout<<"\b},";
}
else if(sum<s)
{
for(i++;i<n;i++)
find_comb(i,j+1,sum);
}

}
void main()
{

clrscr();

cout<<"Enter the lenth of array ";
cin>>n;

cout<<"Enter the numbers......";
for(int i=0;i<n;i++)
{
cin>>a[i];
}

cout<<"Enter the Sum ";
cin>>s;

cout<<"Enter the Number of combinations ";
cin>>N;

for(i=0;i<n-N+1;i++)
find_comb(i,0,0);
cout<<"\b";

getch();

}

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

Here is the algorithm :
K is given number.

-sort the data A[1 to n] O(nlogn)
  for i = 1 to n
     apply finding two data problem whose sum is K-A[i]  and data set is all number except A[i] O(n)

here total time complexity goes to O(n*n)

- sonesh March 15, 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