Hope this small post can help other candidates and have better luck than me :)
Question1: Similar to 791. Custom Sort String, but input range is all unicode characters and every letter on the "input" string is contained in the "order" string. This was my solution:
public String customSort(String input, String order) {
Map<Character, Integer> freq = new HashMap<>();
for(char c : input.toCharArray()) {
freq.merge(c, 1, Integer::sum);
}
StringBuilder sb = new StringBuilder();
for(char c : order.toCharArray()) {
int count = freq.get(c);
while(count > 0) {
sb.append(c);
count--;
}
}
return sb.toString();
}Response fron Interviewer : Asked about edge cases, answered that because of the restrictions there would be no special cases, even if bouth or one of the inputs are empty it would work. Seems to be satisfied with my answer.
Question2: Solve a mathematics expression using only * and + operators no parenthesis. For example:
String input = "42+10*2+4*3+4";
output: 78
Update: I thought of using a stack but interviewer wanted O(1) space solution.
Coulnd't solve it in time, I had the main idea and code was about 80% done, but struggle to find the actual solution and at the end run out of time :(
This is my solution I did after the interview:
public int solve(String input) {
int multiplication = 1;
int sum = 0;
int index = 0;
while (index < input.length()) {
StringBuilder sb = new StringBuilder();
while (index < input.length() && Character.isDigit(input.charAt(index))) {
sb.append(input.charAt(index));
index++;
}
if (index < input.length()) {
int val = Integer.parseInt(sb.toString());
char operator = input.charAt(index);
if (operator == '*') {
multiplication *= val;
} else {
// This must be "+" operator
sum += multiplication*val;
multiplication = 1;
}
} else {
multiplication *= Integer.parseInt(sb.toString());
}
index++;
}
sum += multiplication;
return sum;
}Response fron Interviewer : Said I was time out and asked if I had some questions for him, I said no. During the second questions he throw some hints at me, was helpful but at the same time confusing.
Outcome : Still waiting, but honestly don't think they will continue the process. The interviewer was kinda emotionless and seem to be doing something else while I was coding and explaining my ideas, which is really annoying talking to someone and they don't seem to care. I wish they would get more "friendly" or more helpful engineers, I guess lots of SE at facebook are emotionless.
Update outcome: As expected, got rejected :'( . It actually wasn't that hard, I just had bad luck and couldn't think the actual solution for question2. Funny enough I actually solved the problem within 20 secs immediately after the interview ended, guess I hate interviews....