Amazon Interview Question for Software Engineer / Developers


Team: payments team
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
7
of 11 vote

int index = 0;
		for (int i = 0; i < a.length;) {
			while (sum > k && index < i) {
				sum = sum - a[index];
				index++;
			}
			if (sum == k) {
				for (int k = index; k < i; k++) {
					System.out.print("-" + a[k]);
				}
			}
			sum = sum + a[i];
			i++;
		}

	}

- Naveen November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question doesn't ask about contiguous subarray

- Anjana November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

if we are allowed to use space of O(n), then its time comp O(n).
create aux an array with a[i]=sum of elements upto i in the given array.
now its a simple problem of x-y=k; which can solved in O(n). (aux array is sorted).

- huha November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will u determine the start index.
ur solutions makes an assumption that it will always start at zero.

- Bevan December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

aux is sorted????....how cn u say that because given array is unsorted and may also contain negative integer anywhere in it..

- saraf2206 November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@saraf
if array contain negative numbers too,
then construct a bst with each node containing parent pointer and an other pointer
which points to the middle of the path from ancestor of node to the node.
Hence you can construct the bst in O(n).
now proceed to the problem from there.

- Anonymous November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@saraf
if array contain negative numbers too,
then construct a bst with each node containing parent pointer and an other pointer
which points to the middle of the path from ancestor of node to the node.
Hence you can construct the bst in O(n).
now proceed to the problem from there.

- huha November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashMap is d solution

- Vincent November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

posted my comment on wrong post, please ignore above mentioned comment

- Gaurav November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is unsorted array and we need a sub array of it. So I think it is not allowed to create BST, because we are changing the order of the array.
for example: an array of {1,2,3,7,8,4,5} and k=12 then the sub array would be {2,3,7} and {8,4}
if this is the case then the time complexity in worst case is o(n^2).
I couldn't find better solution.

- Anonymous November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have assumed the input array(hard-coded) , can be taken as user input as well.

- Gaurav November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include<stdio.h>

int main()

{


int arr[]= {1,3,1,3,4,2,6,6,7,10};

int i,j,sum,maxsum,len;
printf("\n entere the sum");
scanf("\n %d", &sum);

len =sizeof(arr)/sizeof(int);

for(i=0;i<len;i++)
{maxsum=0;
for(j=i;j<len;j++)
{
maxsum=maxsum+arr[j];

if(maxsum==sum)
{
printf("\n sum found at %d and between %d and %d", maxsum,i,j);

}
}

}

}

- Gaurav November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
find the sub array of sum K from the the given unsorted array
*/
#include <iostream>
#include <conio.h>
#include <array>
using namespace std;
void getSubArray(int arr[],int arraySize,int * start, int * end, int desiredSum){
	int sum = 0;
	cout<<"initial values of start and end pointer elements: "<<*start<<" "<<*end<<endl;
	
	if(arraySize==1){return;}

	start = arr;
	end = arr+1;
	int count = 1;	
	
	sum = sum + *start + *end;
	
	
	while(count < arraySize){		
		
		if(desiredSum == sum){	
			cout<<"Final subarray is: ";
			while(start<=end){
				cout<<*start<<" ";
				start++;
			}
			return;
		}
		if(sum<desiredSum){
			end++;
			sum += *end; 
		}
		if(sum>desiredSum){
			sum -= *start;
			start++;
		}
		count++;
	}
	cout<<"No sub array found with desired sum"<<endl;
	
}
int main(){
	int arr[5]={2,2,4,1,5};
	int * start= arr;
	int * end= arr + 1;
	int arraySize = sizeof(arr)/sizeof(*arr);	
	
	getSubArray(arr,arraySize,start,end,6);	
	getch();
return 0;
}

o/p:
initial values of start and end pointer elements: 2 2
Final subarray is: 2 4

- sp!de? November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It only works in case of Positive numbers.
In case of Negative numbers it will fail.

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

Th questions address contiguous sequence or sequence in any order? What is meant by sub array?

- Eswar November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

knapsack is the solution...

- Rahul Arakeri November 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Explain ?

- amit November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use DP in order to find the subarray

if(a[n] > Sum)
Subarray(a,n-1,Sum)
else
Subarray(a,n-1, sum-a[n]) || Subarray(a,n-1, sum)

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

If the subarray need not to be contiguous then it is a standard subset sum problem.

- khunima April 05, 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