Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

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.

public static List<String> comments(String codes) {
    List<String> comments = new ArrayList<>();
    if(codes == null) return comments;
    int i = 0, n = codes.length();

    while(i < n) {
        char c = codes.charAt(i);

        //ignore anything quoted
        if(c == '\'' || c == '"') {
            i++;
            while(codes.charAt(i) != c) {
                //when come across a "\\", escape the next char
                if(codes.charAt(i) == '\\') i++;
                i++;
            }
        }
         // '/' is a sign for the start of a comment section
        else if(c=='/'){
            StringBuilder comment = new StringBuilder();
            char type = codes.charAt(++i);
            i++;

            if(type == '*') {
                //  this type /* */ of comment,ends with "*/"
                while(codes.charAt(i) != '*' || codes.charAt(i + 1) != '/') {
                    comment.append(codes.charAt(i));
                    i++;
                }
            } else {
                //  this type // of comment,end with "\\n" or when i == n (at the end of codes)
                while (i < n && ( codes.charAt(i) != '\\' || i + 1 == n || codes.charAt(i + 1) != 'n')) {
                    char a = codes.charAt(i);
                    if(a == '\\') {
                        //corner case: "\\\\n" 
                        //find a  "\\" and if next char is not ‘n’, then escape next char
                        comment.append(codes.charAt(i + 1));
                        i++;
                    }
                    comment.append(a);
                    i++;
                }
            }
            comments.add(comment.toString());
            i++;
        }
        i++;
    }
    return comments;
}

- aonecoding January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
    }
  }
}

- obed.tandadjaja January 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
    }

- joyfeng March 03, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More