Google Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
I believe my code is easier to understand than the other answer, and it handles the problem of when there is a comment inside of a comment
time complexity: O(n)
space complexity: O(1)
import java.util.*;
public class FindCommentsJava {
public static void main(String[] args) {
String s =
"/* //file created by aonecode.com\\n" +
"welcome// to the tech blog */ \\n" +
"// main /*method*/\\n"+
"public static void main(String[] args) {\\n"+
" System.out.println(\"welcome\");//output\\n"+
"}";
try {
ArrayList<String> comments = getComments(s);
System.out.println(comments.toString());
} catch (InvalidSyntaxException e) {
System.out.println(e.getMessage());
}
}
public static ArrayList<String> getComments(String s) throws InvalidSyntaxException {
ArrayList<String> comments = new ArrayList<String>();
boolean currentSingleComment = false;
boolean currentMultiLineComment = false;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length()-1; i++) {
if(s.charAt(i) == '\\' && s.charAt(i+1) == 'n' && currentSingleComment) {
currentSingleComment = false;
comments.add(sb.toString());
sb = new StringBuilder();
i++;
} else if(s.charAt(i) == '*' && s.charAt(i+1) == '/' && currentMultiLineComment) {
currentMultiLineComment = false;
comments.add(sb.toString());
sb = new StringBuilder();
i++;
} else if(currentSingleComment || currentMultiLineComment) {
sb.append(s.charAt(i));
} else if(s.charAt(i) == '/' && s.charAt(i+1) == '/') {
currentSingleComment = true;
i++;
} else if(s.charAt(i) == '/' && s.charAt(i+1) == '*') {
currentMultiLineComment = true;
i++;
}
}
if(sb.length() > 0) throw new InvalidSyntaxException("Comment syntax error");
return comments;
}
public static class InvalidSyntaxException extends Exception {
public InvalidSyntaxException(String message) {
super(message);
}
}
}
static List<String> comments(String s) {
List<String> output = new ArrayList<>();
if (s == null) {
return output;
}
int i = 0;
StringBuilder sb = new StringBuilder();
boolean inSingleComment = false;
boolean inMultiComment = false;
boolean isInQuote = false;
while (i < s.length()) {
if (s.charAt(i) == '\"') {
isInQuote = !isInQuote;
}
// if it is in quote and not in comment, ignore
if (isInQuote && !inMultiComment && !inSingleComment) {
i++;
continue;
}
if (s.charAt(i) == '/' && i+1 < s.length() && s.charAt(i+1) == '*' && !inSingleComment) {
inMultiComment = true;
i++;
} else if (s.charAt(i) == '/' && i+1 < s.length() && s.charAt(i+1) == '/' && !inMultiComment) {
inSingleComment = true;
i++;
} else if (i+1 < s.length() && s.charAt(i) == '\\' && s.charAt(i+1) == 'n') {
if (inSingleComment) {
if (sb.length() > 0) {
output.add(sb.toString());
}
sb = new StringBuilder();
inSingleComment = false;
}
if (inMultiComment) {
sb.append(s.charAt(i));
sb.append(s.charAt(i+1));
}
i++;
isInQuote = false;
} else if (i+1 < s.length() && s.charAt(i) == '*' && s.charAt(i+1) == '/') {
if (inMultiComment) {
if (sb.length() > 0) {
output.add(sb.toString());
}
sb = new StringBuilder();
inMultiComment = false;
}
i++;
} else if (inSingleComment || inMultiComment) {
sb.append(s.charAt(i));
}
i++;
}
if (sb.length() > 0) {
output.add(sb.toString());
}
return output;
}
Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews. All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work. Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us with any questions.
- aonecoding January 27, 2017