## Directi Interview Question for Developer Program Engineers

• 1
of 1 vote

Country: India
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
2
of 4 vote

Suppose you are on index i
--- First find max height between 0 to i-1 , let assume it A
--- Now find max height between i+1 to n, let assume it B

let C = min(A,B)

if(C < height of building i)
than water storage on building i is 0
else
water got stored is (C-A)

do it for all i , 0 to n

you can do it in O(n) time complexity , O(2*n) space complexity using pre-computation.

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

That logic is wrong.
You would fail on an input of say: 131529491.

Instead use a stack and scan through from left to right, with conditions to push or pop buildings onto or from the stack, you'll achieve it in time O(n) then too.

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

I think you meant "else water stored is (C - height of building i)". I would just express it as:

water stored = max (0, C - height of building i)

An interesting follow-up problem to this question is to solve it in 3 dimensions.

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

Find the index of the tallest building, and then go from both ends of the array until it hits the maximum's index.

``````public static int volumeCollected(int[] array) {
if (array == null || array.length <= 2) {
return 0;
}
int maxIndex = findMaxIndex(array);
int length = array.length;
int volume = 0;
int collector = array;
for (int i = 1; i < maxIndex; i++) {
if (array[i] > collector) {
collector = array[i];
} else {
volume += collector - array[i];
}
}
collector = array[length-1];
for (int i = length - 2; i > maxIndex; i--) {
if (array[i] > collector) {
collector = array[i];
} else {
volume += collector - array[i];
}
}
return volume;
}

private static int findMaxIndex(int[] array) {
int max = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = i;
}
}
return max;
}``````

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

Oops, comparison should be array[i] > array[max] in findMaxIndex method

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

for ( int i =1 ; i < n ;++)
{
int priviousmax =a;
int currentmax =a ;
int volume = 0;
if (a[i] > currentmax )
{
currentmax = a[i];
volume + = (i* (currentmax -privousmax )
priviousmax = currentmax;
}
else
volume + = currentmax - a[i] ;
}

Solution is o(n) time complexity, and two varibale for space complexity (o(2))

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.