Amazon Interview Question for Software Engineer in Tests






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

first sort the array in nlgn and then its easy just take two pointers one pointing ats start index of array and one at the end of the array and start checking sum
like this
whwrw a is the integer array givcen
int *p;
int *q;
p=a;
q=a+n-1;
while(p!=q)
{
if(*p+*q==sum)
{printf("(%d,%d)\n",*p,*q);
p++;
q--;
continue;}
if(*p+*q<sum)
{
p++;
continue;
}
q--;
}

- geeks July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if no duplicated pair is allowed?

- dafeilei October 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

find all S-A[i], and stick them in a hash table. Check all A[i] against it.

- memo July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

algo will not work if array has an element S/2.

- DK July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good point. Save the index i together with S-A[i], and check for that too.

- memo July 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we do it in O(n) without extra memory?

- Anonymous July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You could if the array was sorted, but I think it is impossible.

- memo July 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first we should sort d array and calculate sum-a[i] and do binary search for the difference. total runtime takes O(nlogn) i hope

- sharath July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Use Hash table<int,int> myMap
Key = Desired Sum - Item In Array
Value = Index
2) For each Item in Array
if not myMap.exist(Arr[i])
Insert in Map <DesiredSum - Arr[i], index>
else
Print Map<DesiredSum - Arr[i]>.Value, Arr[i]

- Saumya July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void pairofsums(int *d,int length)
{

int pair1=0,pair2=0;
vector<int>vector1;
map<int,int>possibilites;
for(int x=0;x<length;x++)
{
vector1.push_back(d[x]);
}
for(int i=0;i<vector1.size();i++)
{
cout<<vector1[i]<<endl;
for(int f=0;f<vector1.size();f++)
{
if((vector1[i]+d[f])==2)
{
pair1=vector1[i];
pair2=d[f];
possibilites[pair1]=pair2;
}
}
}
map<int,int>::iterator iter;
for(iter=possibilites.begin();iter!=possibilites.end();iter++)
{
cout<<"Pair 1: "<<iter->first<<" Pair 2: "<<iter->second<<endl;
}
}

- FAU_OWLS November 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about-5

- monica April 11, 2014 | 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