## Amazon Interview Question for Dev Leads

Country: United States

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

def find_sub_array(a, s):
for i in range(len(a)):
for j in range(len(a), i, -1):
sub = a[i:j]
total = sum(sub)
if total == s:
return sub
return None

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

``````def find_sub_array(a, s):
for i in range(len(a)):
for j in range(len(a), i, -1):
sub = a[i:j]
total = sum(sub)
if total == s:
return sub
return None``````

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

I think that the idea here is to do this in O(N) as the brute force algorithm take O(N^2). We can achieve an O(N) solution by have 1 loop which runs over the array and looks over a window of the array. This will use the fact that the array contains non negative numbers.

``````def find_continued_sub_arr(A, S):

i, j, sum = 0, 0, 0

while i < len(A) and j < len(A) and sum != S:
sum += A[j]
if sum < S:
j += 1
elif sum > S:
if i < j:
sum -= A[i]
i += 1
else:
i = j = i + 1

return None if sum != S else (i,j)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote
I think that the idea is to use a solution that runs in O(N) which uses the fact that the array contains non-negative numbers. So if we run over the array in one loop which modifies a start and end indices it will do the magic. {{{ def find_continued_sub_arr(A, S): i, j, sum = 0, 0, 0 while i < len(A) and j < len(A) and sum != S: sum += A[j] if sum < S: j += 1 elif sum > S: if i < j: sum -= A[i] i += 1 else: i = j = i + 1 return None if sum != S else (i,j) }}
Comment hidden because of low score. Click to expand.
0
of 0 vote

``````def find_sub_arr(arr, S):
for i1, e1 in enumerate(arr):
sub_sum = e1
if sub_sum == S:
return arr[i1:i1+1]
elif sub_sum > S:
continue

for i2, e2 in enumerate(arr[i1+1:], i1+1):
sub_sum += e2
if sub_sum == S:
return arr[i1:i2+1]
elif sub_sum > S:
break

return []``````

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

``````#include <stdio.h>

int main()
{
int temp,idx,i,start,end;
start = 0;
end = 0;
int a[]={2,6,2,1,4,4};
for (i=0;i<sizeof(a);i++)
{
temp += a[i];
if(temp > 7)
{
i = ++start;
temp = a[i];
}
if (temp == 7)
{
end =i;
break;
}
}
printf("%d %d",start, end);
return 0;
}``````

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

#include <stdio.h>

int main()
{
int temp,idx,i,start,end;
start = 0;
end = 0;
int a[]={2,6,2,1,4,4};
for (i=0;i<sizeof(a);i++)
{
temp += a[i];
if(temp > 7)
{
i = ++start;
temp = a[i];
}
if (temp == 7)
{
end =i;
break;
}
}
printf("%d %d",start, end);
return 0;
}

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

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

int main()
{
int temp,idx,i,start,end;
idx =0;
start = 0;
end = 0;
int a[]={2,0,1,2,4,4};
for (i=0;i<sizeof(a);i++)
{
temp += a[i];
if(temp > 7)
{
i = ++start;
temp = a[i];
}
if (temp == 7)
{
end =i;
++idx;
break;
}
}
if(idx == 0)
printf("xxx");
else
printf("%d %d",start, end);
return 0;
}

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

``````/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

int main()
{
int temp,idx,i,start,end;
idx =0;
start = 0;
end = 0;
int a[]={2,0,1,2,4,4};
for (i=0;i<sizeof(a);i++)
{
temp += a[i];
if(temp > 7)
{
i = ++start;
temp = a[i];
}
if (temp == 7)
{
end =i;
++idx;
break;
}
}
if(idx == 0)
printf("xxx");
else
printf("%d %d",start, end);
return 0;
}``````

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.

### 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.