Amazon Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Phone Interview
I don't think above answer is correct. I think question is to of pattern maching. Sea!tle should be found in dictionary with best matching.
I would use Tries to solve the problem.
Interesting proposal using Tries.
How would you match Sea!tle with Seattle with this approach?
In Tries, I would first search S, then Se, then Sea. Finally once we are sea, i'll search in all child nodes of it.
I think same approach would be implemented in Google also while searching the element.
I used to like Careercup but after seeing the above comment I won't come back here again.
in java
public static String prettyPrint(String sentence) {
// split every word
ArrayList<String> split = new ArrayList<String>(Arrays.asList(sentence.split("\\s")));
String output = "";
boolean cap = true;
boolean newline = false;
// go through each word
for (int i = 0; i < split.size(); i++) {
String each = split.get(i);
int len = each.length();
// check if the last character is a '.', ',', or '!'
switch (each.charAt(len - 1)) {
case '.':
case ',':
case '!':
newline = true;
each = each.substring(0, len - 1) + ".";
}
// if you found an '!' replace it with 't'
each = each.replace("!", "t");
// first word of the sentence must be capitalized
if (cap) {
output += each.substring(0, 1).toUpperCase() + each.substring(1);
cap = false;
} else {
// rest of the words are all in lowercase with a prepending space
output += " " + each.toLowerCase();
}
// add a new line and make sure to mark the caplitalized flag as true
if (newline) {
output += '\n';
cap = true;
newline = false;
}
}
return output;
}
USing strtok :
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.!");
while (pch != NULL)
{
toupper(pch[0]);
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
Not the prettiest solution but ruby solution works
=begin
Give this input: Sea!tle is a nice place. Work Hard! have Fun, make HIStory!
display this output using any language
Seattle is a nice place.
Work hard.
Have fun.
Make history.
=end
def format_statement
str = 'Sea!tle is a nice place. Work Hard! have Fun, make HIStory!'
str = str.split(" ")
str[0] = str[0].gsub('!', 't')
first, i, first_part = str[0..4], 5, first_part = true
puts first.join(" ")
until i == str.size do
if first_part == true
str[i] = str[i].capitalize
first_part = false
elsif
str[i] = str[i].downcase
first_part = true
end
i +=1
end
str = str.join(" ").gsub(",", ".").gsub("!", '.').split(" ")
puts str[5..6].join(" ") + "\n" + str[7..8].join(" ") + "\n" + str[9..10].join(" ")
end
format_statement
void seattleLineParser(String input){
def lines = input.split("[!.,][\\s+]");
for (line in lines){
def newLine = ""
def first = true
for (char_ in line.replace("!", "").toCharArray()){
if (first) { first = false; newLine += char_.toUpperCase() }
else newLine += char_.toLowerCase()
}
println "$newLine."
}
}
seattleLineParser("Sea!tle is a nice place. Work Hard! have Fun, make HIStory!")
public class AmazonSplitString {
public static void main(String[] args) {
String s="Sea!tle is a nice place. Work Hard! have Fun, make HIStory!";
s=s.replaceFirst("!", "t");
modify(s);
}
public static void modify(String s)
{
String[] str=s.split("\\.|\\,|\\!");
for(int i=0;i<str.length;i++)
{
String string=str[i];
string=string.trim();
String result="";
for(int j=0;j<string.length();j++)
{
char c=' ';
if(j==0)
{
c=string.charAt(j);
c=Character.toUpperCase(c);
result+=c;
}
else
{
c=string.charAt(j);
c=Character.toLowerCase(c);
result+=c;
}
}
System.out.print(result+".");
System.out.println();
}
}
}
- Santosh April 22, 2014