Amazon Interview Question
SDETsTeam: kindle
Country: India
Interview Type: In-Person
how many underscores do we have between "ro" and "ss"? is it that the difference between a normal underscore depicting a space and the underscores separating the letters in the words different depending on the number of letters that follow the underscores? If this is not the case then do we have some kind of a dictionary for lookup?
public static String reverseWords(String in) {
StringBuilder out = new StringBuilder();
Pattern p = Pattern.compile("(\\w+)(\\s*)");
Matcher m = p.matcher(in);
while(m.find()) {
StringBuilder word = new StringBuilder(m.group(1));
out.append(word.reverse());
out.append(m.group(2));
}
return out.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "a____de___r";
String newStr = "";
for(int i=0;i<str.length();i++) {
if(i==0 && str.charAt(i)=='_') {
newStr = newStr+" ";
}
if(str.charAt(i)=='_') {
if(newStr.charAt(newStr.length()-1)!=' ') {
newStr = newStr+" ";
continue;
}else {
continue;
}
}else {
newStr = newStr+str.charAt(i);
}
}
System.out.println(newStr);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "a____de___r";
String newStr = "";
for(int i=0;i<str.length();i++) {
if(i==0 && str.charAt(i)=='_') {
newStr = newStr+" ";
}
if(str.charAt(i)=='_') {
if(newStr.charAt(newStr.length()-1)!=' ') {
newStr = newStr+" ";
continue;
}else {
continue;
}
}else {
newStr = newStr+str.charAt(i);
}
}
System.out.println(newStr);
}
class StringProb:
list = []
def removeSpace(self, str):
list = str.split('_')
print (list)
for element in list:
if element is '':
list.remove(element)
print (list)
str1 = ""
for strelement in list:
str1 = str1 + " " +strelement
print(str1)
stringprob = StringProb()
stringprob.removeSpace("Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_worl_d")
class StringProb:
list = []
def removeSpace(self, str):
list = str.split('_')
print (list)
for element in list:
if element is '':
list.remove(element)
print (list)
str1 = ""
for strelement in list:
str1 = str1 + " " +strelement
print(str1)
stringprob = StringProb()
stringprob.removeSpace("Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_worl_d")
make it with all single '_'s
add the length of words on both sides of a '_'
If it is greater than 5 and length of word on right side is not equal to 2 then replace '_' with ' '
else remove the '_'
String str = "Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_wor_d";
//remove ' ' and double and triple dashes
str = str.replace("___" , "_");
str = str.replace("__" , "_");
str = str.replace(" " , "_");
String newStr = "";
String arr [] = str.split("_");
for(int i=0; i < arr.length; i++) {
String words [] = newStr.split(" ");
int nWord = words[words.length -1].length();
if (nWord + arr[i].length() > 5 && arr[i].length() != 2)
{
newStr += " ";
}
newStr += arr[i];
}
System.out.println(newStr);
def remove_space(data):
expected_string = "Amazon web services are widely used accross the world"
exp_str_list = expected_string.split(' ')
data_ary = data.split('_')
print (data_ary)
a = ''
new_string = ''
for i in range(len(data_ary)):
a += data_ary[i]
for j in range(len(exp_str_list)):
if exp_str_list[j] == a:
new_string += exp_str_list[j] + ' '
a = ''
print(new_string)
remove_space("Amazon_w_e_b_services_are___widely__used_acc__ro___ss_the_worl_d")
public static void main(String[] args) {
remove_space("Amazon_w_e_b_services_are___widely__used_acc__ro___ss_the_worl_d");
}
public static void remove_space(String data) {
String expected_string = "Amazon web services are widely used accross the world";
String[] exp_str_list = expected_string.split(" ");
String[] data_ary = data.split("_");
String a = "";
String new_string = "";
for (int i = 0; i < data_ary.length; i++) {
a += data_ary[i];
for (int j = 0; j < exp_str_list.length; j++) {
if (exp_str_list[j].equals(a)) {
new_string += exp_str_list[j] + " ";
a = "";
}
}
}
System.out.print(new_string);
}
public class RemoveUnderscores {
public static void main(String[] args) {
remove_space("Amazon_w_e_b_services_are___widely__used_acc__ro___ss_the_worl_d");
}
public static void remove_space(String data) {
String expected_string = "Amazon web services are widely used accross the world";
String[] exp_str_list = expected_string.split(" ");
String[] data_ary = data.split("_");
String a = "";
String new_string = "";
for (int i = 0; i < data_ary.length; i++) {
a += data_ary[i];
for (int j = 0; j < exp_str_list.length; j++) {
if (exp_str_list[j].equals(a)) {
new_string += exp_str_list[j] + " ";
a = "";
}
}
}
System.out.print(new_string);
}
}
public class string_replace {
public static void main(String args[]) {
String Str = "Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_worl_d";
String Str2 = null;
if (Str.contains("_")) {
Str2 = Str.replaceAll("_", " ");
}
System.out.println("String is: " + Str2.toString());
}
}
public class string_replace {
public static void main(String args[]) {
String Str = "Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_worl_d";
String Str2 = null;
if (Str.contains("_")) {
Str2 = Str.replaceAll("_", " ");
}
System.out.println("String is: " + Str2.toString());
}
}
public class string_replace {
public static void main(String args[]) {
String Str = "Amazon_w_e_b_services are___widely__used_acc__ro___ss_the_worl_d";
String Str2 = null;
if (Str.contains("_")) {
Str2 = Str.replaceAll("_", " ");
}
System.out.println("String is: " + Str2.toString());
}
}
public static void main(String[] args) {
String expectedString = "Amazon web services are widely used accross the world";
String inputString = "Amazon_w_e_b_services_are___widely__used_acc__ro___ss_the_worl_d";
String[] expected = expectedString.split(" ");
String[] input = inputString.split("_");
String outputString = "";
int j=-1;
for(String i : expected) {
String temp = input[++j];
if(i.equalsIgnoreCase(temp))
outputString = outputString + " " + temp;
else{
String temp1 = "";
while(!i.equalsIgnoreCase(temp)) {
temp1 = temp;
j++;
temp1=temp1+input[j];
temp=temp1;
}
outputString = outputString + " " + temp;
}
}
System.out.println(outputString);
}
import java.util.*;
public class MyClass{
public static void main(String [] args){
String str= "\"Amazon_w_e_b_services are_widelyused_accro__ss_the_worl_d\"";
System.out.println(str);
StringBuilder sb = new StringBuilder(str);
sb.replace(7,8," ");
sb.replace(13,14," ");
sb.replace(26,27," ");
sb.replace(37,38," ");
sb.replace(47,48," ");
sb.replace(51,52," ");
sb.insert(33," ");
str=String.valueOf(sb);
System.out.println(str);
str=str.replace("_","");
str=str.replace("\"" ,"");
System.out.println(str);
}
}
import java.util.*;
public class MyClass{
public static void main(String [] args){
String str= "\"Amazon_w_e_b_services are_widelyused_accro__ss_the_worl_d\"";
System.out.println(str);
StringBuilder sb = new StringBuilder(str);
sb.replace(7,8," ");
sb.replace(13,14," ");
sb.replace(26,27," ");
sb.replace(37,38," ");
sb.replace(47,48," ");
sb.replace(51,52," ");
sb.insert(33," ");
str=String.valueOf(sb);
System.out.println(str);
str=str.replace("_","");
str=str.replace("\"" ,"");
System.out.println(str);
}
}
Without dictionary lookup, wouldn't it be impossible for this problem because we have no way of knowing whether the underscores separate a word or simply letters in a word.
- prudent_programmer February 23, 2018For example, how would we know that "are" and "widely" are two different words? Does three underscores denote separation between two words and single underscore denote separation between two letters in a word? Anyone have thoughts on this?