Google Interview Question for SDE1s


Country: United States




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

This is written in Go:

func match(regex []byte, input []byte) bool {
	if len(regex) == 0 {
		if len(input) == 0 {
			return true
		} else {
			return false
		}
	}

	if len(regex) == 1 || (regex[1] != '+' && regex[1] != '*') {
		return len(input) > 0 && (regex[0] == input[0] || regex[0] == '.') && match(regex[1:], input[1:])
	} else if regex[1] == '*' {
		return match(regex[2:], input) || (len(input) > 0 && (regex[0] == input[0] || regex[0] == '.') && match(regex, input[1:]))
	} else {
		cr := make([]byte, len(regex))
		copy(cr, regex)
		cr[1] = '*'
		return len(input) > 0 && (regex[0] == input[0] || regex[0] == '.') && match(cr, input[1:])
	}

}

- Julian January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There could be DP approach. f[i][j] = true / false (can we parse first j characters of the string with first i characters of the regexp). We also can represent regexp as automata, but that is optional.

- Alex February 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you give an idea of how to do with DP for regex parsing.

- peeterparker April 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RegexMatch {

	public static boolean matches(String pat, String str){
		int patlen = pat.length();
		int strlen = str.length();
		if(patlen == 0) return strlen == 0;

		char patFirst = pat.charAt(0) ;

		if(patlen == 1 ){
			return strlen == 1 && charsMatch(patFirst, str.charAt(0));
		}

		char patSecond = pat.charAt(1);
		if( patSecond != '+' && patSecond  != '*'){ // just a character
			return strlen > 0 && charsMatch(patFirst, str.charAt(0))
					&& matches(pat.substring(1), str.substring(1));
		} else {
			if(patSecond == '*'){
				return (strlen > 0 &&  charsMatch(str.charAt(0),patFirst) 
						&& matches(pat, str.substring(1)) 
						|| matches(pat.substring(2), str));
			} else { // +
				return strlen > 0 && charsMatch(str.charAt(0),patFirst) && 
						matches("" + patFirst +'*' + pat.substring(2), str.substring(1));
			}
		}



	}

	
	
	private static boolean charsMatch(char ch1, char ch2){

		if(ch1 == '.' || ch2 == '.') return true;
		return ch1 == ch2;
	}

	public static void main(String[] args) {
		test("ac*a*ca", "aca");
		test("ac+a*ca", "aca");
		test("ac*a*ca", "acaca");
		test("ac+a*ca", "acaca");
		test("ac+a+ca", "acaca");
		test("ac*a*cab", "acaca");
		test(".+a", "acaca");
		test(".+b", "acaca");
		test(".*world!+", "hello world!!!");
		test(".*woald!+", "hello world!!!");
		
	}



	private static void test(String pattern, String str) {
		System.out.println("String " + str+ " matches pattern " + pattern +": " + matches(pattern, str));
	}
	


}

- konst May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not good at dynamic programming, so the border conditions may not be true

Let String is s
and Pattern p
for . or character 
   if (p[j] == '.' || s[i] == p[j]) {
      DP[i+1][j+1] = DP[i][j];
   } else if (p[i] == '+') {
    DP[i+1][j+1] = DP[i][j+1] || DP[i+1][j];
 } else if (p[i] == '*') {
    // do back track as we may have already consider pattern like c*(c already matched)
    DP[i][j] = DP[i-1][j] || DP[i][j-1];
 } else {
    DP[i+1][j+1] = false;
   }

- ali.kheam July 08, 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