Linkedin Interview Question for Software Engineer / Developers






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

package linkedin;

public class PrintText {
	public static void main(String[] args) {
		String input = "Cracking the Coding Interview: 150 Programming Interview Questions and Answers focuses on mastering the programming interview. Topics include: strategies to handle tough algorithm questions, preparation techniques, behavioral questions, and 150 programming interview questions and answers.";
		int width = 20;
		
		int pos = 0;
		StringBuilder sb = new StringBuilder();
		
		String[] words = input.split(" ");
		for (String w : words) {
			if (pos + w.length() > width) {
				sb.append('\n');
				pos = 0;
			}
			sb.append(w);
			sb.append(' ');
			pos += w.length() + 1;
		}
		
		System.out.println(sb);
	}
}

- metacret August 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It says left-right justified, which I take to mean, 'left /and/ right justified'. If that's so, you're not right-justifying your output.

- Guy Smiley April 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suppose we must also vary the space between 2 words in a different way for each line so as to make it almost L characters or else a really long word at the end of the line will end up in the next line leaving a long white space towards the end of the prev line.

- Anonymous August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string inputString( "very very long long this is a very very long long string" );
std::string tempBuff;

std::stringstream inputStringStream( inputString );

unsigned int currentCharCount = 0;
unsigned int tempCharCount = 0;

while ( inputStringStream >> tempBuff )
{
tempCharCount = currentCharCount + tempBuff.size( );

if (tempCharCount >= MAX_CHAR_IN_LINE )
{
std::cout<<"\n";

//reset
currentCharCount = 0;
tempCharCount = 0;
}

std::cout<<tempBuff<<" ";

currentCharCount = currentCharCount + tempBuff.size( ) + 1;
}

- Anonymous July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
  * given a long string str, print in line, no breaking of words, left-right, at most L characters per line
  * str = ab cd ef, L = 2, output ab\ncd\nef\n
  * str = ab d e hl, L = 3, output abd\nehl\n
  * str = abc efg, L = 4, output abc\nefg\n
  * str = a, L = 2, output a\n
  */
 void format_print(const char* str, int L)
 {
    char buffer = new char[L];
    int buffer_head = 0;
    int buffer_size = 0;
    for (; *str; str++)
    {
        if (buffer_size == L)
        {
           
           //output from buffer_head, for buffer_size
           if (*str == ' ')
           {
              last_word_right_bound = buffer_size - 1;
           }
           for (int i = 0; i < buffer_size; i++)
           {
               printf("%c", buffer[(buffer_head + i - 1 + L) % L]);
               if (i == last_word_right_bound)
               {
                  for (int j = i + 1; j < buffer_size; j++)
                  {
                      printf(" ");
                  }
                  printf("\n");              
               }
           }
           buffer_head = buffer_size =  0;
        }
        {
           buffer[(buffer_head + buffer_size - 1 + L) % L] = *str;
           if (*str == ' ' && buffer_size > 0)
           {
              last_word_right_bound = buffer_size - 1;
           }
           buffer_size++;
        }
    }
    if (buffer_size > 0)
    {
           bool only_space = true;
           for (int i = 0; i < buffer_size; i++)
           {
               printf("%c", buffer[(buffer_head + i - 1 + L) % L]);   
               only_space &= (buffer[(buffer_head + i - 1 + L) == ' ');
           }
           for (; !only_space && i < L; i++)
           {
               printf(" ");
           }
           printf("\n");
    }
 }

- wjian@yahoo-inc.com September 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For both left and right justification, first do usual step of trying to put the words in each line with single space. Then, if we end up with n spaces at end of line and the next word is larger than n-1, then we need to distribute the n spaces evenly across the line.

For this even distribution, if current spaces upto last word in the line = x, then do
intpart = n/x
remainderpart = n mod x.

Add intpart spaces to all spaces, then starting from first or last, add the reminderpart spaces until they are exhausted.

E.g., if there are 27 spaces at end, and next word is 30 characters, also suppose current line has 11 words or 10 spaces upto last word, then we need to distribute these 27 spaces over the 10 spaces. So 27/10 = 2 int part, 7 reminder part. So we ad 2 spaces to all 10 spaces, and then remaining 7 spaces, we add i space each to 7 spaces starting from beginning or end.

- venkat325 November 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Another approach could be to do the following:
1. Have 2 pointers - begin and end.
2. Set begin to the start of the long text.
3. Advance end L units. Check to see if end is now a blank space. If not, move back end until a blank space is encountered.
4. Print all characters from begin to end. Print new line character.
5. Do end++; begin = end;
6. Repeat steps 1-6 until end hits the end of the long text.

- Wolverine October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is missing right-justify.
You need to check how many spaces are at the end of the line and add extra space to existing spaces so the words are right justified as well. When adding extra spaces it should be evenly distributed to make it look better.

- Anonymous March 14, 2013 | Flag


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