Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public boolean isMatch(String s, String p) {
      int count = 0;
      for(int i = 0; i < p.length() ;i++){
        if(p.charAt(i)!='*') count++;
      }

      if(count > s.length()) return false;

      boolean[][]dp = new boolean[p.length() + 1][s.length() + 1];

      dp[0][0] = true;
      for(int i = 1; i <= p.length();i++){
        if(p.charAt(i-1) == '*')
          dp[i][0] = dp[i-1][0];  
        else dp[i][0] = false;
      }
     
     for(int i = 1; i <= s.length(); i++){
        for(int j = 1; j <= p.length(); j++){
          if(p.charAt(j -1) == '?') dp[j][i] = dp[j-1][i - 1];
          else if(p.charAt(j -1) == '*'){
            dp[j][i] = dp[j][i-1] | dp[j-1][i] | dp[j-1][i-1];
          }
          else if(p.charAt(j -1) == s.charAt(i -1)) dp[j][i] = dp[j - 1][i-1];
          else dp[j][i] = false;
        }
      }
      return dp[p.length()][s.length()];
    }

- mrsurajpoudel.wordpress.com November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PatternMatch {

public static void main(String[] args) {
Pattern p = Pattern.compile("a-z + *");
Matcher m = p.matcher("a-z");
Boolean b = m.matches();

if(b)
System.out.println("matches");
else
System.out.println("does not match");

}

}

- umu March 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PatternMatch {

	public static void main(String[] args) {
		Pattern p = Pattern.compile("a-z + *");
		Matcher m = p.matcher("a-z");
		Boolean b = m.matches();
		
		if(b)
			System.out.println("matches");
		else
			System.out.println("does not match");

	}

}

- umu March 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'+' instead of '?'
This was my solution to problem "Wild Card Matching' in oj.leetcode.com

- mrsurajpoudel.wordpress.com November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'+' instead of '?'
This was my solution to problem "Wild Card Matching' in oj.leetcode.com

- mrsurajpoudel.wordpress.com November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is my java code:
private boolean isMatch( String s, String regex)
{
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
boolean b=m.find();
return b;
}

- Anonymous November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess the interviewer won't ask for which library function you will use

- arsragavan December 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

following may be updated to meet ur requirements..

public class PatternMatcher {

	public static void main(String[] args) {
		String str = "This  is a sample string";
		String pattern = "Th*is +";
		System.out.println(isMatchFound(str,pattern));
		pattern = "a*This";
		System.out.println(isMatchFound(str,pattern));
		pattern = "*This";	//valid pattern
		System.out.println(isMatchFound(str,pattern));
		pattern = "+This";	//invalid pattern
		System.out.println(isMatchFound(str,pattern));
	}
	
	public static boolean isMatchFound(String str, String pattern){
		return isMatchFound(str, pattern, 0, 0);
	}
	
	private static boolean isMatchFound(String str, String pattern, int strInd, int patInd){
		if(strInd < str.length()){
			if(patInd==pattern.length()){
				return true;
			} else {
				if(pattern.charAt(patInd)=='*'){
					int i=patInd-1;
					for(;i>=0;i--)
						if(pattern.charAt(i)!='*' && pattern.charAt(i)!='+')
							break;
					if(i<0){
						//return false;
						return isMatchFound(str, pattern, strInd, patInd+1);
					} else if(pattern.charAt(i)==str.charAt(strInd)){
						return isMatchFound(str, pattern, strInd+1, patInd);
					} else {
						return isMatchFound(str, pattern, strInd, patInd+1);	//match may have been found before, don't worry if its not found as well
					}
				} else if (pattern.charAt(patInd)=='+') {
					int i=patInd-1;
					for(;i>=0;i--)
						if(pattern.charAt(i)!='*' && pattern.charAt(i)!='+')
							break;
					if(i<0){
						return false;
						//return isMatchFound(str, pattern, strInd, patInd+1);
					}else if(pattern.charAt(i)==str.charAt(strInd)){
						return isMatchFound(str, pattern, strInd+1, patInd);
					} else if(pattern.charAt(i)==str.charAt(strInd-1)){
						return isMatchFound(str, pattern, strInd, patInd+1);	//cur str char still needs to match so not doing strInd+1
					} else {
						return isMatchFound(str, pattern, strInd, 0);	//match not found, start pattern from 0
					}
				} else {	//current pattern char not + nor *
					if(pattern.charAt(patInd)==str.charAt(strInd)){
						return isMatchFound(str, pattern, strInd+1, patInd+1);
					} else {
						if(patInd+1<pattern.length() && pattern.charAt(patInd+1)=='*'){
							return isMatchFound(str, pattern, strInd, patInd+1);	//match not found
						} else {
							return isMatchFound(str, pattern, strInd+1, 0);	//match not found
						}
					}
				}
			}
		} else {
			if(patInd<pattern.length())
				return false;
			else
				return true;
		}
	}

}

- kn November 18, 2014 | 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