Amazon Interview Question for SDE1s


Country: India
Interview Type: Phone Interview




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

private void findPeak(int[] arr) {
for(int i=1;i<arr.length-1;i++) {
if(arr[i-1] < arr[i] && arr[i] > arr[i+1]) {
System.out.println("Peak element "+arr[i]+" and the index of array is "+i);
}
}
}

public static void main(String[] args) {
//int arr[] = {5, 10, 20, 15};
//int arr[] = {10, 20, 15, 2, 23, 90, 67};
int arr[] = {1, 3, 20, 4, 1, 0};
FindPeakElements findPeakElements = new FindPeakElements();
findPeakElements.findPeak(arr);
}

- Abdul May 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PeakElement {

public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = { 10, 25, 15, 35, 36, 37 };
System.out.println(findPeakElement(arr, 0, arr.length - 1));
}

private static int findPeakElement(int[] arr, int start, int end) {
// TODO Auto-generated method stub
int mid = (start + end) / 2;

if ((mid == 0 || arr[mid] > arr[mid - 1])
&& (mid == arr.length - 1 || arr[mid] > arr[mid + 1])) {
return arr[mid];
} else if (mid > 0 && (arr[mid] < arr[mid + 1])) {
return findPeakElement(arr, mid + 1, arr.length - 1);
} else {
return findPeakElement(arr, 0, mid - 1);
}
}
}


Reference - geeksforgeeks

- Sibendu Dey May 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone define what is a peak element in an array ?
If I assume it to be the element, before which the elements are smaller than it and after which the elements are larger than it, then will an array have only one peak element ? Or can there be peaks in an array?

- Nousheen May 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Binary Search. Time complexity is O(log n).

- RoBa May 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import java.util.Arrays;

public class ArrayPeakElement {

	public static void main(String[] args) {
		
		int a[] = {1,2,3,4,5,6,1,6,9};
		
		Arrays.sort(a);
		System.out.println("Peak element in the array is" + a[a.length-1]);
		
	}
}

- suresh May 05, 2016 | 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