Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
Java Code
public static void main(String[] args) {
String string="epic is a healthcare company";
printVertical(string);
}
public static void printVertical(String input){
Queue<String> stringQueue=new LinkedList<String>();
String [] string=input.split(" ");
for (int i = 0; i <string.length ; i++) {
stringQueue.add(string[i]);
}
for (int i = 0; i <string.length ; i++) {
String stringPrint=stringQueue.poll();
System.out.printf("%s\n",stringPrint);
}
public void convertHorizontalStringToVertical(String[] strArr) {
int max = 0;
for (int i = 1; i < strArr.length; i++) {
int temp = strArr[i].split(" ").length;
if (max < temp) {
max = temp;
}
}
String[][] output = new String[max][strArr.length];
for (int i = 0; i < strArr.length; i++) {
String[] temp = strArr[i].split(" ");
for (int j = 0; j < max; j++) {
if (j < temp.length) {
output[j][i] = temp[j];
} else {
output[j][i] = " ";
}
}
}
for (int i = 0; i < output.length; i++) {
System.out.println(Arrays.toString(output[i]));
}
}
Output:
[epic, interviewing, print]
[is, for, this]
[a, software, vertically]
[healthcare, developer, sentence]
[company., , by]
[ , , sentence.]
We know that we will have encountered a sentence by the time we have reached a period in a string. So, what we can do is create a queue, enqueue all strings until a period is reached. Then we create a new queue and repeat the process. There should be one queue for each sentence. Finally, starting from the first queue until the last queue, dequeue and print, then start from the first queue again until the last queue- dequeue and print. Continue this process until all queues are empty
- SB March 19, 2015