## Amazon Interview Question for Software Developers

• 0

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;``````

}

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.

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``````

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.