Google Interview Question for SDE1s


Country: United States




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

public static boolean matcher(String pattern, String seq) {
        if (pattern == null && seq == null) return true;
        int lenPattern = pattern.length(), lenSeq = seq.length();
        int ptr1 = 0, ptr2 = 0;
        while (ptr1 < lenPattern && ptr2 < lenSeq) {
            char ch1 = pattern.charAt(ptr1);
            char ch2 = seq.charAt(ptr2);
            if (ch1 >= '1' && ch1 <= '9') {
                ptr1++;
                int rep = ch1-'0';
                ptr2 += rep;
            } else {
                if (ch1 == ch2) {
                    ptr1++; ptr2++;
                } else {
                    return false;
                }
            }
        }
        if (ptr1 != lenPattern || ptr2 != lenSeq) return false;
        return true;
    }

- Aim_Google January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is a bug in this code. you are only considering case when single digit represents a number. There could be more digits like "15" and you can skip 15 chars.

- CodeReviewer February 26, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isValid(String pattern, String matcher)
{
   for(int i=0,j=0;i<pattern.length()&&;j<matcher.length();i++,j++)
{

if(Character.isDigit(pattern.charAt(i))
{
 int number=Integer.parseInt(pattern.charAt(i));
 if(number==1)
  continue;
 while(number>1)
     j++;
}
else if(patter.chartAt[i]!= matcher.charAt[i])
  return false;
else
  return true;
}

- Anonymous January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isValid(String pattern, String matcher)
{
   for(int i=0,j=0;i<pattern.length()&&j<matcher.length();i++,j++)
{

if(Character.isDigit(pattern.charAt(i)))
{
 int number=Integer.parseInt(pattern.charAt(i));
 if(number==1)
  continue;
 while(number>1)
     j++;
}
else if(patter.chartAt[i]!= matcher.charAt[i])
  return false;
else
  return true;
}

}

- hsg January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isValid(String pattern, String matcher)
{
   for(int i=0,j=0;i<pattern.length()&&j<matcher.length();i++,j++)
{

if(Character.isDigit(pattern.charAt(i)))
{
 int number=Integer.parseInt(pattern.charAt(i));
 if(number==1)
  continue;
 while(number>1)
     j++;
}
else if(patter.chartAt[i]!= matcher.charAt[i])
  return false;
else
  return true;
}
}

- hsgill88 January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isMatch(@Nonnull final String matcher, @Nonnull final String text) {
        StringBuilder matchPattern = new StringBuilder();
        for (char c : matcher.toCharArray()) {
            if (c >= 48 && c <= 57) {
                matchPattern.append("[a-z]{").append(c).append("}");
            } else {
                matchPattern.append(c);
            }
        }
        Pattern pattern = Pattern.compile(matchPattern.toString(), Pattern.CASE_INSENSITIVE);
        return pattern.matcher(text).matches();
    }

- Scavi January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static bool IsPatternMatchingWithStringV2(string pattern, string str)
        {
            int i = 0 /* Pattern index*/,j=0 /*String index*/;

            for (i = 0; i < pattern.Length && j < str.Length; i++)
            {
                if (Char.IsDigit(pattern[i]))
                {
                    j += Convert.ToInt32(pattern[i].ToString());
                }
                else
                {
                    if (pattern[i] != str[j])
                    {
                        return false;
                    }

                    j++;
                }
            }

            return true;
        }

- Baz Jir Ipekyuz January 13, 2018 | 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