Google Interview Question
Software Engineer / DevelopersCountry: Switzerland
Interview Type: Phone Interview
That's certainly a warm up. Python lets you answer this essentially in one line:
def makeNumeronym(word):
return (word[0] + str(len(word[1:-1])) + word[-1]) if (isinstance(word, str) and len(word) >= 3) else word
This would be even shorter if it didn't do type checking and length checking.
public class Main {
public static void main(String[] args) {
String s = "house";
String expected = makeNumeronym(s);
System.out.println(expected);
}
private static String makeNumeronym(String s) {
int x = s.length() -1;
String y = s.substring(1, x);
int z = y.length();
String Numeronym = s.charAt(0) + String.valueOf(z) + s.charAt(x);
return Numeronym;
}
}
///
public class Main {
public static void main(String[] args) {
String s = "house";
String expected = makeNumeronym(s);
System.out.println(expected);
}
private static String makeNumeronym(String s) {
int x = s.length() -1;
String y = s.substring(1, x);
int z = y.length();
String Numeronym = s.charAt(0) + String.valueOf(z) + s.charAt(x);
return Numeronym;
}
}///
public class Main {
public static void main(String[] args) {
String s = "house";
String expected = makeNumeronym(s);
System.out.println(expected);
}
private static String makeNumeronym(String s) {
int x = s.length() -1;
String y = s.substring(1, x);
int z = y.length();
String Numeronym = s.charAt(0) + String.valueOf(z) + s.charAt(x);
return Numeronym;
}
}}
public class Main {
public static void main(String[] args) {
String s = "house";
String expected = makeNumeronym(s);
System.out.println(expected);
}
private static String makeNumeronym(String s) {
int x = s.length() -1;
String y = s.substring(1, x);
int z = y.length();
String Numeronym = s.charAt(0) + String.valueOf(z) + s.charAt(x);
return Numeronym;
}
}
public class Main {
public static void main(String[] args) {
String s = "house";
String expected = makeNumeronym(s);
System.out.println(expected);
}
private static String makeNumeronym(String s) {
int x = s.length() -1;
String y = s.substring(1, x);
int z = y.length();
String Numeronym = s.charAt(0) + String.valueOf(z) + s.charAt(x);
return Numeronym;
}
}
I would propose the following solution:
Btw, isn it that marcus -> m4s? Is it something the interviewer expected?
- autoboli April 23, 2015