RealSelf Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




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

Here you go :
rose-hulman.edu/Users/faculty/young/CS-Classes/csse220/200820/web/Programs/Markov/justification.html

- NoOne October 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This was a very open ended question and I'm guessing they are looking to see what kind of assumptions the candidate makes and whether all possible base cases have been covered.
I did not make it through this test despite having submitted a working solution. I am pretty sure that means there were either test cases that I missed handling or that I made incorrect assumptions.
Following are the assumptions I made:
1) Delimiters or separators between words are only one or more spaces.
2) The given input could be a bad string, meaning there could be trailing or leading spaces (OR) words could be separated by one or more than one spaces.
3) If the string cannot be justified perfectly, justify as much as possible.
4) If the string cannot be justified perfectly, after as much justification as possible leave the additional buffer empty at the trail.
5) If the buffer is insufficient or the input string is invalid, return the input string itself.
6) If there is no justification necessary (ex - if the input string is just one word and the buffer size is large), leave the rest of the buffer empty (leave spaces)

Following is code that goes along with all my assumptions:

/*
	 * This is the method that will justify the line.
	 * It will return a justified result as a string of the given buffer length.
	 * If the input does not allow to justify the line, then the line itself is returned.
	 */
	public String justify(String inputLine, int bufferLength)
	{
		//If the inputLine happens to be invalid we return the given line itself.
		if(inputLine==null)
			return inputLine;
		
		int inputStringLength = inputLine.length();
		
		//If the inputLine happens to be invalid we return the given line itself.
		if(inputStringLength<=0)
			return inputLine;
		
		int numberOfWords = 0;
		int effectiveInputStringLength = 0;
		
		//There may be trailing spaces leading to the first word in given line.
	    int indexOfFirstWord = getCharIndexOfNextWord(inputLine,inputStringLength,0);
		
		/*Counting the number of words assuming the following about the input: 
		 * 1) two words may be separated by more than one space characters
		 * 2) the first word in input need not start from the first character (could be a trail of spaces)
		 * 3) the only possible delimiter between 2 words is one or more spaces
		 * 4) The last word may be followed by one or more spaces
		 */
		for(int charIndex = indexOfFirstWord; charIndex<inputStringLength; charIndex++)
		{
			if(inputLine.charAt(charIndex) == ' ')
			{
				numberOfWords++;
				charIndex = getCharIndexOfNextWord(inputLine,inputStringLength,charIndex);
				if(charIndex==-1)
				{
					numberOfWords--;
					break;
				}
					
			}
			effectiveInputStringLength++;
		}
		
		numberOfWords++;
		int numberOfSpacesNeeded = numberOfWords - 1;
		
		//effectiveInputStringLength is the length of the line as it would be if there was only one space between 2 words and no trailing spaces.
		effectiveInputStringLength = effectiveInputStringLength + numberOfSpacesNeeded;
		
		//If the provided buffer length is too small or just equal.
		if(bufferLength<=effectiveInputStringLength)
		{
			System.out.println("Buffer Length is too small to justify the input line.");
			return inputLine; // returning input line itself
		}
		
		int extraBufferAvailable = bufferLength - effectiveInputStringLength;
		int numberOfExtraPossibleSpaces = 0;
		int numOfSpacesAtTheEndOfBuffer = 0;
		
		if(numberOfSpacesNeeded!=0)
		{
			//We can justify perfectly
			if(extraBufferAvailable % numberOfSpacesNeeded == 0)
			{
				System.out.println("We will be able to justify this line accurately");
				numberOfExtraPossibleSpaces = extraBufferAvailable/numberOfSpacesNeeded;
			}
			
			//We can justify only as much as possible. Last word ends before last available buffer index.
			else
			{
				System.out.println("We will NOT be able to justify this line accurately. But"
						+ " we will justfiy as much as possible such that the first word starts"
						+ "from the first character but the last word ends before the last available buffer index.");
				numberOfExtraPossibleSpaces = extraBufferAvailable/numberOfSpacesNeeded;
				numOfSpacesAtTheEndOfBuffer = extraBufferAvailable % numberOfSpacesNeeded;
			}
			numberOfExtraPossibleSpaces = numberOfExtraPossibleSpaces+1;
		}
		
		else
		{
			numOfSpacesAtTheEndOfBuffer = extraBufferAvailable;
		}
		
		StringBuilder justifiedStringBuilder = new StringBuilder(bufferLength);
		char currentChar;
		
		for(int charIndex = indexOfFirstWord; charIndex<inputStringLength; charIndex++)
		{
			currentChar = inputLine.charAt(charIndex);
			
			if(currentChar != ' ')
			{
				justifiedStringBuilder.append(currentChar);
			}
			else
			{
				charIndex = getCharIndexOfNextWord(inputLine,inputStringLength,charIndex);
				if(charIndex==-1)
				{
					break;
				}
				
				addSpacesToResult(justifiedStringBuilder,numberOfExtraPossibleSpaces);
				currentChar = inputLine.charAt(charIndex);
				justifiedStringBuilder.append(currentChar);
			}
		 }
		
		if(numOfSpacesAtTheEndOfBuffer>0)
		{
			addSpacesToResult(justifiedStringBuilder,numOfSpacesAtTheEndOfBuffer);
		}
		
		return justifiedStringBuilder.toString();
	}
	
	

	private void addSpacesToResult(StringBuilder justifiedStringBuilder,
			int numberOfExtraPossibleSpaces) {
		for(int noOfSpaces = numberOfExtraPossibleSpaces; noOfSpaces>0; noOfSpaces--)
		{
			justifiedStringBuilder.append(' ');
		}
		
	}



	private int getCharIndexOfNextWord(String inputLine, int inputStringLength,
			int currentIndex) {
		for(int charIndex = currentIndex; charIndex<inputStringLength; charIndex++)
		{
			if(inputLine.charAt(charIndex)!=' ')
			{
				return charIndex;
			}
		}
		
		return -1;
	}
	public static void main(String[] args) {
		CodingChallenge mycodeChallenge = new CodingChallenge();
		String[] input = {"The quick brown fox jumps over the lazy dog.",
				"The", "     The     ", null, "     The quick brown fox jumps over the lazy dog.",
				"The quick brown fox        jumps over the lazy dog.     ", 
				"The quick      brown      fox jumps over the      lazy dog."
		};
		String res;
		for(String s:input)
		{
			res = mycodeChallenge.justify(s,52);
			System.out.println(res);
		}
	}

It would be very helpful if anyone had suggestions about where I may have faltered in my code or test cases or any of the assumptions made.

- teli.vaibhav October 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that the buffer length is greater that the text length.

let bufferLength = length of the buffer.
Split the text into array based on space as delimiter and ignore empty strings.
Count the number of words in the array. Let it be wordCount.
Count the cumulative sum of length of the characters in the array. Let it be textLength.

AvailableSpaces = bufferLength - textLength;

Number of spaces required between first and (wordCount-1) words
spaceCount = Math.Floor( AvailableSpaces / wordCount - 1);

Number of spaces between the last two words
lastSpaceCount = AvailableSpaces - (spaceCount * (wordCount - 2));

Use the above values to print out the words with the calculated spacing.

- Ratish November 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that the buffer length is greater that the text length.

let

bufferLength

= length of the buffer.
Split the text into array based on space as delimiter and ignore empty strings.
Count the number of words in the array. Let it be wordCount.
Count the cumulative sum of length of the characters in the array. Let it be textLength.

AvailableSpaces = bufferLength - textLength;

Number of spaces required between first and (wordCount-1) words

spaceCount = Math.Floor( AvailableSpaces / wordCount - 1);

Number of spaces between the last two words

lastSpaceCount = AvailableSpaces - (spaceCount * (wordCount - 2));

Use the above values to print out the words with the calculated spacing.

- Ratish P November 02, 2016 | 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