Google Interview Question
Developer Program EngineersIf out of order means any change in increasing or decreasing order of ASCII values of individual characters in the string, then its straight forward by comparing the first two characters and identifying the order is increasing or decreasing and accordingly proceed from third location till they go out of order.
It its something like Special characters comes in one group, number comes in one and similarly A-Z in one and a-z in another group and when a sequence of characters from one group is disturbed by occurrence of a character from another group defines the out of order, then we need change our logic in terms of checking for range of ASCII values for each group.
But I am not sure if my understanding of out of order is right. Please post ur comments.
String contains only letters either upper case or lower case
ABCDAFGH the we should return 4
ABCDaF then we should return 4
if we encounter any letter other than a-z or A-Z the method should return ERROR
if we are using first two elements to find the order, care must be taken when the first two elements are same.
if we are using first two elements to find the order, care must be taken when the first two elements are same.
Ask the definition of "OUT OF ORDER"
Below are the examples of OUT OF ORDER
1. ABCDEFP.... (Difference 1)
2. ACEGIKR.... (Difference 2)
3. AEIOUAEIOP ... (Vowel &Consonents)
4. ABECIDOFL ... (Vowel & Consonets sequence)
etc...
public class test {
public static void main(String[] args) {
String a = "nopsdv";
int prev = (int)a.charAt(0);
int curr = (int)a.charAt(0);
for(int i=0; i<a.length();i++) {
curr = (int)a.charAt(i);
if(curr<prev) {
System.out.println("char:"+a.charAt(i)+" index:"+i);
break;
}
else
prev = curr;
}
}
}
public int findIndexOutOrder(String str) {
String lowStr = str.toLowerCase();
char pre =' ';
if(lowStr.length()>0) {
pre =lowStr.charAt(0);
if(pre<'a' || pre>'z')
return -1;
}
for(int i=1;i<lowStr.length();i++) {
char cur = lowStr.charAt(i);
if(cur<'a' || cur>'z')
return -1;
if(cur<pre)
return i;
pre = cur;
}
return -1;
}
def method(String inputString):
order = "none"
inputStringList = list(inputString)
if int( inputStringList[0] ) <= int( inputStringList[1] ):
order = "increasing"
elif:
order = decreasing
for preIndex,pre in enumerate( inputStringList[:len(inputStringList)-1] ):
nextIndex = preIndex + 1
if order =="increasing":
if inputStringList[preIndex] > inputStringList[nextIndex]:
flag = false
elif order == "decreasing":
if inputStringList[preIndex] <= inputStringList[nextIndex]:
flag = false
if flag = False:
return False
elif:
return True
<pre lang="" line="1" title="CodeMonkey64343" class="run-this"> public int inorderCheck(char [] c){
int sum0=0,sum1=0;
if(c.length==0) return -1;
for(int i=0;i<c.length;i++){
sum1=0;
if(i==0)continue;
sum0=Character.toUpperCase(c[i-1])-'A';
char ch= Character.toUpperCase(c[i]);
sum1= ch-'A';
if(sum1<sum0)return i;
}
return -1; // the string is in order
}
</pre><pre title="CodeMonkey64343" input="yes">
char []characters1 = new char[]{'D','O','P','a','Z'};
char []characters2 = new char[]{};
char []characters3 = new char[]{'D','O','P','q','Z'};
char []characters4 = new char[]{'p','D','O','P','a','Z'};
System.out.println( new InOrderCheck().inorderCheck(characters1));
System.out.println( new InOrderCheck().inorderCheck(characters2));
System.out.println( new InOrderCheck().inorderCheck(characters3));
System.out.println( new InOrderCheck().inorderCheck(characters4));</pre>
What the hell are those guys thinking about merge sort?
- idiot February 26, 2011Just use scan the string, O(n)