Microsoft Interview Question


Country: -




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

The idea is that you need to keep checking for the word length each time you read a word and see if the remaining length of characters that can fit in the line is greater than the word length and print it else you need to print the characters tpo next line.

I am giving pseudo code:

int remLength = m;
String[] str = S.split(" ");
String tempStr = null;
for(i=0; i<str.length; i++)
{
tempStr = str[i];
if(tempStr.length <=remLength)
{
System.out.print(tempStr);
remLength -= tempStr.length;
}
else
{
i--;
remLength = m;
System.out.println();
}
}

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think words needs to be arranged in a line to avoid as much spaces as possible
this is what i got .. give ur comments

hash <size of word,words>
total_words = S;
longest_word;
shortest_word;
while(total_words>0)
{
	len = --m;//1 for next line
	while(m>0)
	{
		if(m>longest_word)
		{
			word+=hash(longest_word);  
			remove_word(hash);	
			m-=hash(longest_word).size +1 ; //for space
			if(!hash(longest_word))
			{
				find_next_longest_word(&longest_word);
			}
		    total_words--;
		}
		else
		{
			while(!hash(m))
			{   if(m<shortest_word)
					break;
				m--;
			}
			word+=hash(m); 
			m-=hash(m).size +1 ; //for space
			remove_word(hash(m));	
			total_words--;
			   
		}
	}
	addline(word);
	flush(word);
}

- Master Fuji October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void printString(char*s, int m)
{
char* p = s;
int count = 0;
while (*p != '\0')
{
char *q = p;
while (*q != ' ' && *q != '\0')
{
q++;
count++;
}
if (count < m)
{
printword(p, q); //prints word from p to q-1
p = q+1;
}
else
{
cout << endl;
count = 0;
}
}

}

- Raghavendra October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. The best way would be implementing our own buffer that overflows whenever the size reaches 'm' (of course handling any partial words). If that is too much for the problem the we can write a loop that does the processing like:

const char* givenString = "Align S words in lines where each line can contain at most m characters per line. Words should never be split between two lines.";
  
  std::ostringstream wordBuffer;
  std::ostringstream lineBuffer;
  std::ostringstream lines;
  const char *currentChar = givenString;
  const int m = 10;

  int countWordsPerLine = 0;
  while (('\0' != *currentChar) && (0 != m))
  {

    wordBuffer << *currentChar;
    if(' ' == *(currentChar+1) || ' ' == *(currentChar))
    { //push a complete word
      lineBuffer << wordBuffer.str().c_str();
      wordBuffer.clear();
      wordBuffer.str(std::string());
    }
    if(m <= lineBuffer.tellp() ||
       m <= lineBuffer.tellp() + wordBuffer.tellp())
    { // move to next line
      lines << lineBuffer.str().c_str() << "::";
      lineBuffer.clear();
      lineBuffer.str(std::string());
    }
    ++currentChar;
  }
  lines << wordBuffer.str().c_str();
  std::cout << lines.str().c_str();

- WallStreetCup October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey50448" class="run-this">/*
Test Input:
I am a final year BE student.
9
Output:
I am a
final
year BE
student.
*/
import java.util.Scanner;
import java.util.Vector;
class Test{
String inpStr;
int aInt;
private Vector<String> aVecStr = new Vector<String>();
Test(int mm, String ss){
inpStr = ss;
aInt = mm;
}
public void convert(){
String[] words = inpStr.split(" ");
String str = new String("");
for(String tmp: words){
if(str.length() + tmp.length() > aInt){
aVecStr.add(str);
str = "";
}
str += tmp+" ";
}
if(str.length()>0)
aVecStr.add(str);
}
public void print(){
for(String s: aVecStr){
System.out.println(s);
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
Test t = new Test(9,"I am a final year BE student.");
t.convert();
t.print();
}
}</pre><pre title="CodeMonkey50448" input="yes">
</pre>

- monish.gupta1 October 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

include<stdio.h>
#include<conio.h>
void main(){
clrscr();
char *input="hello there is a microsoft";
int m=10;
int rem=0;
int *index=new int[50];
int indexer=0;
for(int i=0;input[i]!='\0';i++){
if(input[i]==' '){
index[indexer++]=i;

}
}
int count=0;
for(int j=0;input[j]!='\0';j++){
if(input[j]==' '){
count++;
if((index[count]-j)+rem>m){
printf("\n");
rem=0;
continue;

}
}
printf("%c",input[j]);
rem++;
}
getch();


}

- Algo baba October 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
  const int m = 5;

  string line;
  getline(cin, line);
  stringstream ss(line);

  string word;
  int n = 0;
  ss >> word;
  n = word.size();
  cout << word;
  while (ss >> word)
    {
      if (n+word.size()+1>m)
        {
          cout << "\n";
          n=word.size();
        }
      else
        {
          cout << " ";
          n+=word.size()+1;
        }
      cout << word;
    }
  return 0;
}

- krwq November 03, 2011 | 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