Google Interview Question
SDE1sCountry: United States
index = 0;
speed = 0;
void updateForA(){
if(speed == 0){
speed =1;
}
if(index ==0){
index = 1;
}else{
index ++;
speed = speed *2;
}
}
void updateForR(){
if(index > 0){
index = index - 1;
speed = 0;
}
}
int goThroughString(String s){
String[] arr = s.toChar();
for(String instruction : arr){
if(instruction.equals(A)){
updateForA();
}else{
updateForR();
}
if(speed == 0){
return index; // assuing the position starts at 0;
}
}
SOP("Car Doesnt stop");
}
E.G.
AAR ->
firstindex speed nextindex nextspeed
A 0 0 1 1
A 1 1 2 2
R 2 2 1 0
Check if below works-
- kunal December 18, 2017