## 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;
}``````

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

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

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

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

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

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

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;
}``````

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

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));
}

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

{done}

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;
};``````

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!

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

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

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

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.

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

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));
}

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));
}

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));
}
}

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));
}

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));
}
}

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

done

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));

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;
}``````

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

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).

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;
}

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;
}

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;
}

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

Thank you very much for so quickly answers!

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
}

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;
}

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;
}

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;
}

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);
}

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);
}``````

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();
}``````

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)));
}``````

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)));``````

}

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)));``````

}

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)));``````

}

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)));
}``````

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;
}``````

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;
}``````

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;
}

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)));``````

}

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))); }``

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

``````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)));
}``````

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;
}``````

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)));
}``````

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)));
}``````

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;
}``````

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;``````

}

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));
}``````

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));
}``````

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

Not supposed to throw NPE. rather return -1;
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;``````

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;
}``````

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.

### 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.