## Directi Interview Question for Senior Software Development Engineers

Country: India
Interview Type: Phone Interview

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

I think you would need a double for loop. In the outer loop, you iterate through all the houses to find one to begin at, then in the inner loop with the starting house defined in the outer loop, you will go through the rest of the houses and check to see what the best values you can make are, by continuously multiplying by the gems you encounter and only updating the values if you have visited an even number of red houses. I make an assumption that you can't have negative gems but that you can encounter a house with 0 gems, in which case your range will mess up, so you break out of the inner loop since there's nothing more you can do if you continue (since you will just multiply by 0).

``````int[] rangeHouse(char[] house, int[] houseGem) {

int[] optRange = new int[2];
int bestVal = 0;

int currentStart = 0;
int currentEnd = 0;
int currVal = 1;

int redCount = 0;

for (int i = 0; i < house.length; i++) {

redCount = 0;
currVal = 1;

currentStart = i;
currentEnd = i;

for (int j = i; j < house.length; j++) {

if (houseGem[j] == 0) {

break;
}

else {

currVal *= houseGem[j];
if (house[j] == 'R') { redCount++; }

if (redCount % 2 == 0 && currVal > bestVal) {

optRange[0] = currentStart;
optRange[1] = currentEnd;
bestVal = currVal;
}
}
}
}

return optRange;
}``````

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

You can take assumption that there is no negative value and gems are not 0.
your solution seems to be correct.
But can you do it in O(n) ?

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

If gems are not less than 1 then the value will never be able to decrease, so simply keep updating the value in one loop, assuming we have already passed even number of Reds

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

Agree with @Skor. You can simply keep updating the value in loop and if redCounter is odd then increment the start point by 1 and output the bestVal. Otherwise print the bestVal

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

use 2pointer algorithm can get On solution

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.