## Amazon Interview Question for SDE-2s

• 0

Country: India
Interview Type: In-Person

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

Scan from Right for the O(N) Solution.
Scan all the elements from right to left in array and keep track of maximum till now. When maximum changes it’s value, print the same as Leader.

``````void FindLeader ( int Arr[], int ArrSize ) {
int max = Arr[ArrSize-1] ;
for ( int i = ArrSize-1; i >= 0; i– ) {
if ( Arr[i] >= max ) {
cout << Arr[i] << ” ” ;
max = Arr[i] ;
}
}
cout << endl ;
}``````

Time Complexity: O(N)

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

This is smart. The problem become like a local max problem.

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

``````private static void findLeader(int[] arr) {
int max=Integer.MIN_VALUE;
for(int i=arr.length-1;i>=0;i--)
{
if(arr[i]>max)
{
System.out.print("  "+arr[i]);
max=arr[i];
}
}
}``````

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

``````void findLeaders ( int[] arr) {
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>=arr[j]){
if(j==arr.length-1){
System.out.print(arr[i]);
break;
}
continue;
}
break;
}
}
System.out.print(arr[arr.length-1]);
}``````

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

``````private static void findArrayLeaders(int arr[]) {
if (arr == null || arr.length == 0) {
System.out.println("Invalid Inputs");
return;
}
int i = 0, len = 0, leader = 0;
len = arr.length;
for (i = len - 2; i >= 0; i--) {
}
}
}``````

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

``````function findLeader(inputArr) {

for (var i = 0; i < inputArr.length; i++) {
}
}
}

for (var j = pos; j < inputArr.length; j++) {
if (inputArr[pos] < inputArr[j]) {
return false;
}
}
return true;
}``````

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

``````public class GetArrLeader {

public static void main(String[] args) {
int[] intArr = {12,14,10,12,2};
// should print 14,12 and 2
}

private static void printLeaders(int[] intArr) {

for(int i=0;i<(intArr.length)-1;i++) {
int j = i+i;
boolean bool = false;
while (j<intArr.length) {
if(intArr[i]>intArr[j++])
bool = true;
else {
bool = false;
break;
}
}
if(bool) System.out.println(intArr[i]);

}
System.out.println(intArr[intArr.length-1]); // last one is always considered
}

}``````

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

var ele=[13,17,5,4,6,2];
ele=ele.reverse();

ele.map( function(item) {
}

})

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

Javascript code, you can run in browser console. just copy and paste it in browser console to see output.

``````var ele=[13,17,5,4,6,2];
ele=ele.reverse();

ele.map( function(item) {
}

})

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

``````int main() {
int arr[6] = {13, 17, 5, 4, 6, 2};
int max = 0;
int index = 5;

for (; index >= 0; index--) {
if (arr[index] >= max) {
printf("%d ", arr[index]);
max = arr[index];
}
}

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.