Google Interview Question for Software Engineer in Tests






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

Laksmi and nanmaga - what kind of answers are you giving - they want a regular Expression NOT CODE!!!

Regular expression to detect occurence of 'foo' in the string is - (.*)(foo)(.*)
Regular expression to detect of 'foo' exactly once in the string is - (.*)(foo)(.*)(fo[^o])(.*)

- DeziDude October 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

grep (.*)(foo)\{1\}(.*) will match exactly one occurance of the pattern foo in the string.

- daisy898 February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can some one give some hints please?

- Java Coder October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;


public class nonRepeatingExpression {

private String src;
private HashMap<Character,Integer> myMap = new HashMap<Character,Integer>();
private char [] charArr;

public nonRepeatingExpression(String src) {
// TODO Auto-generated constructor stub
this.src = src;
}
private void constructHashMap(){

if(src == null){
System.out.println("Invalid String");
return;
}

charArr = src.toCharArray();

for(int i = 0; i < src.length()-1; i++){
Integer count = myMap.get(charArr[i]);
if(count == null){
myMap.put(charArr[i],1);
}
else{
myMap.put(charArr[i],count+1);
}
}
}


private void findUnique(){
ArrayList<Integer> valueList = new ArrayList<Integer>(myMap.values());
ArrayList<Character> keyList = new ArrayList<Character>(myMap.keySet());


for(int i = 0; i < valueList.size();i++ ){
if(valueList.get(i) == 1){
System.out.println(keyList.get(i));
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
nonRepeatingExpression obj = new nonRepeatingExpression("jkkjFoohkkh");
obj.constructHashMap();
obj.findUnique();
}

}

- Java Coder October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I also have another solution if the pattern Foo is given before hand and we have a source string in which we shud find if the pattern is unique.

package google;

import java.util.HashMap;

public class FindUniqueRegExp {

private String src;
private String pattern;
private HashMap<String, Integer> myMap = new HashMap<String, Integer>();

public FindUniqueRegExp(String src, String pattern) {
// TODO Auto-generated constructor stub
if (src != null && pattern != null) {
this.src = src;
this.pattern = pattern;
}
}

private void createHashMap() {
int index = 0;
if (src == null) {
System.out.println("Invalid Source");
return;
}
if (pattern == null) {
System.out.println("Invalid Pattern");
return;
}

while ((index + pattern.length()) <= (src.length() - 1)) {

String key = (String) src
.substring(index, index + pattern.length());
Integer count = myMap.get(key);

if (count == null) {
myMap.put(key, 1);
// index++;
} else
myMap.put(key, count + 1);
index++;
}
}

private void isUnique() {

if (pattern != null) {
int value = myMap.get(pattern);
if (value == 1) {
System.out.println("Pattern " + pattern + " is unique!");
} else {
System.out.println("Pattern " + pattern + " is not unique!");
}
}

}

public void findIfUnique() {
createHashMap();
isUnique();
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

FindUniqueRegExp obj = new FindUniqueRegExp("jhkhFookjjk", "Foo");
obj.findIfUnique();

}

}

- Java Coder October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The way the question is framed is confusing. The first part hints about finding that substring whereas the example considers a substring....Could someone pls explain wht r the i/o for the question? THanks

- nanmaga October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The way the question is framed is confusing. The first part hints about finding that substring whereas the example considers a substring....Could someone pls explain wht r the i/o for the question? THanks

- nanmaga October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The way the question is framed is confusing. The first part hints about finding that substring whereas the example considers a substring....Could someone pls explain wht r the i/o for the question? THanks

- nanmaga October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

grep "\(foo\)*\(foo\)\(foo\)*"

- Anonymous November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

grep "[^\(foo\)]*\(foo\)[^\(foo\)]*"

- Anonymous November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think they are asking for an idea, and it should be solved using suffix tree

- Anonymous June 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct!

- Anonymous July 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL.

Suffix trees and regular expressions don't really mix well.

- LOLer July 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

irb(main):044:0> a
=> "afoob"
irb(main):045:0> b
=> "afoobfooc"
irb(main):046:0> puts "test" if a =~ /(.*foo.*){2,}/
=> nil
irb(main):047:0> puts "test" if b =~ /(.*foo.*){2,}/
test
=> nil
irb(main):048:0>

- test September 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(?<!(foo))(foo)(?!(foo))

^ negative lookaround.

- Anonymous October 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

all r wrong solution

- dd October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

RE= (L-{f,o,o})*.f*.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.foo.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.F*.(L-{f,o,o})*

- dd October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

RE= (L-{f,o,o})*.f*.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.foo.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.F*.(L-{f,o,o})*

- dyhsdg October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

RE= (L-{f,o,o})*.f*.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.foo.(L-{f,o,o})*.(fo)*.(L-{f,o,o})*.F*.(L-{f,o,o})*

- dyhsdg October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey49564" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

</pre><pre title="CodeMonkey49564" input="yes">
</pre>

- Anonymous October 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is

\w*?foo\w*?\s(?!foo+)

- Sarah Ross February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

foo([^f]*f*.?[^fo]{1})*

- Anonymous December 16, 2013 | 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