Amazon Interview Question for Software Engineer / Developers






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

The code is

#include<stdio.h>
#include<stdlib.h>
/* 
   Assume the array is in reverse sort order...
   { 15, 14, 13, 12, 11, 8, 3, 1 }
*/

void FindSum(int* arr, int len, int sum)
{
  int i=0,j=len-1;
  int diff = 0 ;
  while ( i < j )
  {
    if ( arr[i] + arr[j]  < sum )
    {
      j--;
    }
    else if ( arr[i] + arr[j] > sum )
    {
      i++;
    }
    else
    {
      printf("%3d(%d) + %3d(%d) = %3d\n",arr[i],i,arr[j],j,sum);
      return ;
    }
  }
  printf("Go Away!\n");
}
int main(int argc, char** argv)
{
  int i ;
  int* arr;
  int sum = 0 ;
  arr = (int*)malloc(sizeof(int)*(argc-2) );
  sum = atoi(argv[argc-1]);
  for ( i = 1 ; i < argc -1; i++)
  {
    arr[i-1] = atoi(argv[i]);
  }
  FindSum(arr,argc-2,sum);
}

/***
~/Codes$ ./a.out 15 14 13 12 11 8 3 1 24
13(2) + 11(4) = 24
~/Codes$ ./a.out 15 14 13 12 11 8 3 1 4
3(6) + 1(7) = 4


*/

- Lord Darth Plaguies June 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The two numbers you find is a valid pair. But not the first valid pair

- Anonymous July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

suppose array A[0...N)
For i = [0, N)
IF Min<=(givenValue-A[i])<=Max
IF BinarySearch(givenValue-A[i]) is true
Print pair

O(NlgN)

- bbs59 June 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findval(int *arr,int len,int val)
{
	if(len=2)return 0;
	int half=len/2;
	if((arr[half]+arr[half-1])<val)
		return half+findval(arr+half,len-half,val);
	else if(half>1&&arr[half-1]+arr[half-2]>=val)
		return findVal(arr,half,val);
	else
		return half-1;
}

- Tiak July 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can find out first element in O(log(n!)),take first element say s make binary search for (value-s) on n-1 elements then take second one say s1 & make binary search for (value-s1) on n-2 elements & so on ....hence it would be best with O(log(N)) & worst with O(log(n!)).

- mohit September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

generally we talk about worst case complexity and o(log(n!)) is a very complex algorithm.
simple one can be like this with order o(n).
assuming increasing order of list.left anr right are leftmost and rightmost indices.

if((left+right)<sum)
left++;
else if((left+right)>sum)
right--;
else return(left,right)

- parkhi September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it has best case O(1) complexity.

- parkhi September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

was wondering if we can use maps here.. that would not use the fact that the given array is sorted.. comments ?

- hash December 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First sort the array and then take each element and use binary search to find the value of sum-array_element_value. Time Complexity O(n lg n)

- Anonymous August 29, 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