## Amazon Interview Question for SDE-3s

Country: United States

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

``````def findIndex(arr):
starting = 0
ending = len(arr)
for i in range(len(arr)-1):
if(arr[i] > arr[i+1]):
starting = i
break

for i in range(len(arr)-1,-1,-1):
if(arr[i] < arr[i-1]):
ending = i
break
print(starting)
print(ending)``````

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

Upvoted, only works if there is some amount of sorting.
It breaks for inputs like : [ 6,1,2,5,4,3,2,1 ], essentially unsorted inputs.

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

Actual problem can be solved, for all inputs this way , worthy of SDE 3 tag. Kudos Amazon.

``````/*
select domain of inversions,
and imagine starting with (0,0) which is sorted ascending.
*/
def find_if_whatever( array ){
N = size(array)
if ( N < 2 ) return true // boundary condition
// now, anything else.
domains = list()
domain = { 's' : 0, 'e'  : 0 , 'st' : true }
for ( i : [ 1 : N ] ){
sorted = ( array[domain.e] <= array[i] )
if ( sorted == domain.st ){
domain.e = i
} else {
// end domain, create domain
domains += domain
domain = { 's' : i, 'e'  : i , 'st' : sorted }
}
}
// now am done ...
domains += domain
println( jstr( domains ) ) // debug???
// now, well, well...
D = size( domains)
if ( D > 3 ) return false // no go... right?
// this is a clear go
if ( D == 1 ) return true
// now, when D=2, we have.. problems.. so
if ( D == 2 ){
if ( domains[0].st && !domains[1].st ) return array[domains[0].e] <= array[domains[1].s]
if ( !domains[0].st && domains[1].st ) return array[domains[0].s] <= array[domains[1].s]
return false
}
// now when D==3, so... larger problems...
if ( domains[0].st && domains[2].st ) {
return array[domains[0].e] <= array[domains[1].e] &&
array[domains[1].s] <= array[domains[2].s]
}
return false // done
}

println( find_if_whatever([ 1, 10,-1 ]) )
println( find_if_whatever([ 1, 2,3,4,8,7,6,9  ]) )
println( find_if_whatever([ 1,2,3,50,40,90,10,23,99  ]) )``````

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

Are you sure this is correct? In your second example, you wouldn't realize that the direction changed until you reached 7. In this case, domain[0].e is 8 whereas domain[1].s is 7. Wouldn't it return false even though it should be true?

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.