Google Interview Question for Developer Program Engineers


Country: United States




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

static int func(String s, char a, char b) {
    for (int i = 1; i < s.length(); i++)
        if (s.charAt(i) == a || s.charAt(i) == b)
            return (a == b) ? a : i;
    return -1;
}

- Anonymous April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));

- kamal April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));

- kamal April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

noam,

Aren't you missing a couple of cases?


The function does the following:
As soon as we find an instance of either a, or b, stop the while loop.

If we locate a and b at the same place, i.e. they are the same letter, past index zero, return min(a, b).

If we find either a or b, the while loop terminates, and we return the index of the one we found.

If we find neither a nor b, OR both or one of them is found only at index zero, return -1. Finding them at index zero is the equivalent of not finding them at all. That's why we should start the search from index 1, not zero.

static int func(String s, char a, char b) 
{
   for (int i = 1; i < s.length(); i++) 
   {
      char ch = s.charAt(i);
      if (ch == a || ch == b)
      {
         return a == b ? Math.min(a, b) : i;
      }
   }

   return -1;
}

- observer April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private int stringIndex(String str)
{
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++)
{
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}

- kamal April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{done}

- kamal April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int ai = s.indexOf(a);
		int bi = s.indexOf(b);
		return ai != -1 && bi != -1? Math.min(ai,bi) : ai + bi +1;

- ArtD April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

module.exports = function (s, a, b) {
	var regex = new RegExp("^([^" + a + b + "]*(" + a + "|" + b + ")).*$");
	var result = regex.exec(s);
	if (!result) {
		return -1;
	}
	return result[1].length -1;
};

- srterpe May 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
{
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if (ch == a || ch == b)
return i;
}

return -1;
}

my solution!

- noam.nta April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is not the perfact answer and what about math.min

- Anonymous April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is not perfact answer and what about math.min

- Anonymous April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But how does this take into account the case where your iterator encounters the larger of a and b first? You're returning i immediately.

- Killedsteel April 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As per question first need to find out the max position of A and Max position of B then other logic

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String str){
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++){
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
System.out.println("A Index : "+aindex+" BIndex : "+bindex);
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String str){
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++){
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
static int func(String str){
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++){
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
System.out.println("A Index : "+aindex+" BIndex : "+bindex);
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}
}

- Kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String str){
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++){
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
System.out.println("A Index : "+aindex+" BIndex : "+bindex);
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
private int stringIndex(String str)
{
int aindex =0, bindex=0;
for(int i=0;i<str.toCharArray().length;i++)
{
if(str.charAt(i)=='a')
aindex = i;
if(str.charAt(i)=='b')
bindex = i;
}
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
}
}

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

done

- kamal April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));

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

My solution (although it's 6 lines with some cheating ;) )

// some test cases...
public static void main(String[] args) {
        System.out.println(function("124609", '5','5'));
        System.out.println(function("124609", '2','0'));
        System.out.println(function("124609", '2','4'));
    }

    public static int function(String s, char a, char b) {
        int bestMatchIdx = -1;
        for(int i=0; i<s.length(); i++) {
            if(s.charAt(i) == a || s.charAt(i) == b) {
                bestMatchIdx = (bestMatchIdx > 0 && s.charAt(i) > s.charAt(bestMatchIdx))?bestMatchIdx:i;
            }
        }return bestMatchIdx;
    }

- Killedsteel April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The logic is basically at each match, to check if bestMatchIdx was set before. If it was, compare it with current value, and if currentValue < previousValue, change bestMatchIdx to point to i. Else, leave it as is.

In the end, return bestMatchIdx (it was initialized to -1, so if no assignment is done, it will return with -1).

One limitation of the above however, is I don't need to continue matching through the string if my second match happened immediately. However, that may need additional lines of code (i.e. > 6).

- Killedsteel April 13, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == a || s.charAt(i) == b)
return i;
}
return -1;
}

- Alex Yao April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int func(String s, char a, char b) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == a || s.charAt(i) == b)
return i;
}
return -1;
}

- Alex Yao April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int func(String s, char a, char b) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == a || s.charAt(i) == b)
return i;
}
return -1;
}

- Ina0.0 April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thank you very much for so quickly answers!

- noam.nta April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are 3 categories of conditions:-

1. We don't find both variables after position 0. don't care if we find any char variable at index '0' or not---------------return -1
2.Find either char variable first but only one. For both sub-conditions return index found.
3.Both char are same i.e. a=b so aIndex=bIndex. So return char variable a or b

Code:
static int func(String s, char a, char b) {
for (int i = 1; i < s.length(); i++) ///loop for checking Condition 2 and 3
if (s.charAt(i) == a || s.charAt(i) == b) ///Note loop starts from index 1
return (a == b) ? a : i; ///check between condition 2 or 3
return -1; ///Left with condition 1
}

- Anonymous April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
{
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if ((ch == a || ch == b)) && i != 0)
return i;
}
return -1;
}

- Anonymous April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
{
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if ((ch == a || ch == b)) && i != 0)
return i;
}
return -1;
}

- zp April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
{
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if ((ch == a || ch == b)) && i != 0)
return i;
}
return -1;
}

- vincent April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
{
if (s.isEmpty()) return -1;
int aIndex=s.indexOf(a);
int bIndex=s.indexOf(b);
return aIndex != 0 && bIndex != 0 ? Math.min(aIndex, bIndex) : Math.max(aIndex, bIndex);
}

- Anonymous April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b)
    {
       if (s.isEmpty()) return -1;
       int aIndex=s.indexOf(a);
       int bIndex=s.indexOf(b);
       return aIndex != 0 && bIndex != 0 ? Math.min(aIndex, bIndex) : Math.max(aIndex, bIndex);
    }

- Ramki April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
	Matcher m = Pattern.compile("(.+)(\\Q" + a + "\\E|\\Q" + b + "\\E).*").matches(s);
	return m == null ? -1 : a == b ? a : m.group(1).length();
}

- Anonymous April 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));
  }

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));

}

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));

}

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));

}

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));
  }

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
		if(s != null) {
			for(int i = 0; i< s.length(); i++){
				if(s.charAt(i) == a || s.charAt(i) == b) {
					return i;
				}
			}
		}
		return -1;
	}

- Anonymous April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
		if(s != null) {
			for(int i = 0; i< s.length(); i++){
				if(s.charAt(i) == a || s.charAt(i) == b) {
					return i;
				}
			}
		}
		return -1;
	}

- solve April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
if(s != null) {
for(int i = 0; i< s.length(); i++){
if(s.charAt(i) == a || s.charAt(i) == b) {
return i;
}
}
}
return -1;
}

- Anonymous April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b):
           (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1)));

}

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos) { if (pos == str.length) return -1; return (str[pos] == 'a' && str[pos] == 'b')? Math.min(a, b): (str[pos] == 'a'? pos: (str[pos] == 'b'? pos: func(str, a, b, pos + 1))); }

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == a && str[pos] == b)? Math.min(a, b):
           (str[pos] == a? pos: (str[pos] == b? pos: func(str, a, b, pos + 1)));
  }

- Koustav Chattterjee April 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) {
		if(s != null) {
			for(int i = 0; i< s.length(); i++){
				if(s.charAt(i) == a || s.charAt(i) == b) {
					return i;
				}
			}
		}
		return -1;
	}

- solve April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == a && str[pos] == b)? Math.min(a, b):
           (str[pos] == a? pos: (str[pos] == b? pos: func(str, a, b, pos + 1)));
  }

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(char str[], char a, char b, int pos)
  {
    if (pos == str.length)
      return -1;
    return (str[pos] == a && str[pos] == b)? Math.min(a, b):
           (str[pos] == a? pos: (str[pos] == b? pos: func(str, a, b, pos + 1)));
  }

- Koustav Chattterjee April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int func(String s, char a, char b){
		int ai = s.indexOf(a);
		int bi = s.indexOf(b);
		return ai != -1 && bi != -1? Math.min(ai,bi) : ai + bi +1; 
	}

- artem.drozdoff April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) 
{
	int i=0;
	while(s[i]!='\0' && s[i]!=a && s[i]!=b)
		i++;
	return s[i]=='\0' ? -1 : i;

}

- Anil April 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int func(String s, char a, char b) throws NullPointerException {
		return Math.min(s.indexOf(a),s.indexOf(b));
	}

- Derekpk May 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This has only two lines:-)

static int func(String s, char a, char b) throws NullPointerException {
		return Math.min(s.indexOf(a),s.indexOf(b));
	}

- DerekPK May 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not supposed to throw NPE. rather return -1;
What about this case return Math.min(a, b); ??
My try --
-----

static int func(String s, char a, char b) 
{ 
if (s.isEmpty()) return -1; 
if (s.contains(String.valueOf(a)) && s.contains(String.valueOf(b)) )  return Math.min(a, b); ;
if (s.contains(String.valueOf(a)) || s.contains(String.valueOf(b)) ) return 1;
return -1;

- infinity May 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The confusing part is Math.min(a, b) I think the intention for this is to ask to the interviewer, and could be Math.min(aIndex, bIndex); But assuming is correct in the way it is this is my solution.

static int func(String s, char a, char b)
{
    if (string.IsNullOrEmpty(s)) return -1;

    char[] strArray = s.ToCharArray();

    if (a == b) return a;

    for (int i=0; i < strArray.Length; i++)
        if (strArray[i] == a || strArray[i] == b)
            return i;

    return -1;
}

- hnatsu March 13, 2016 | 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