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

- SK January 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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) ?

- SK January 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- SK January 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Vib January 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use 2pointer algorithm can get On solution

- enigma January 22, 2015 | 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