Directi Interview Question for Developer Program Engineers


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.

- Sourabh Jain November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- NitroBuster November 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi November 30, 2014 | Flag
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[0];
		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;
	}

- Anonymous November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous November 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for ( int i =1 ; i < n ;++)
{
int priviousmax =a[0];
int currentmax =a [0];
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))

- Amit Singh gaurav November 28, 2014 | 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