Microsoft Interview Question
Country: -
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);
}
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();
<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>
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();
}
#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;
}
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.
- Anonymous October 07, 2011I 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();
}
}