Amazon Interview Question for Software Developers


Country: India




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

int maxWater(int[] arr)
{
	if(arr == null || arr.length < 3) {
		return -1;
	}

	int leftKeep = 0;
	int maxWater = 0;
	int leftCol = 0;
	for (int i = 1; i < arr.length - 1; i++) {
		// left + curr removed
		
		int maxRmvCurr = Math.max(Math.min(leftCol, arr[i + 1]) * 2, leftKeep + Math.min(arr[i - 1], arr[i + 1]));
		maxWater = Math.max (maxRmvCurr, maxWater);
		leftKeep = Math.max(leftKeep,Math.min(arr[i],leftCol));
		leftCol = arr[ i - 1];
	}

	return maxWater;

}

- divm01986 October 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Compute leftmax and rightmax arrays. leftmax[i] denotes max in arr[0..i]. rightmax[i] denotes max in arr[i..n-1].
2. compute extra[] for each tower.
extra[i] = ( (min(leftmax[i], rightmax[i]) - arr[i] > 0) ? arr[i] : min(leftmax[i], rightmax[i]))
3. compute the ans as in the normal algorithm.
ans += ( (min(leftmax[i], rightmax[i]) - arr[i] > 0) ? min(leftmax[i], rightmax[i]) - arr[i] > 0 : 0)
4. now the final result would be ans + firstmax(extra) + secondmax(extra).
Please let me know if there is anything wrong in this approach.

- Raghu October 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Let's define a function to capture rainwater
arr = [2,0,4,0,6,0,2,0,1,0]

def trap_rain_water(arr):
	#Initialize water variable
	water = 0
	#Iterate through array and find out volume of water captured at current bar
	for i in range(1, len(arr)-1):
		vol_at_current_bar = min(max(arr[:i]), max(arr[i:])) - arr[i]
		if vol_at_current_bar > 0:
			water += vol_at_current_bar
	return water

- luvneries December 20, 2018 | 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