Google Interview Question
Developer Program EngineersCountry: United States
return aindex==bindex ? -1 :(aindex>0 && bindex>0 ?Math.min(aindex, bindex):Math.max(aindex, bindex));
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;
}
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));
}
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!
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));
}
{
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));
}
}
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));
}
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;
}
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).
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
}
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))); }
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));
}
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;
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;
}
- Anonymous April 12, 2015