View Single Post
jobyts
jobyts is offline
#20
Feb17-12, 05:34 AM
P: 216
These are typical google/facebook interviews.

I was successfully able to answer question #6 in one of my interviews.
6) You have two robots placed randomly on the number line. When they land, they place a marker. You are to program the robots so that they meet, and you are given the following 3 commands: Go left, go right, or if you are on a marker, change strategy. Specify the two strategies so that the robots eventually meet.
while(!markerSet) {

go left;
go left;
go right;
set marker;
};
while(!peerFound) {
go left;
}

7) Give an algorithm to reverse the order of the letters of each word in a sentence. Do not change the order of the words. For instance if your sentence is "I like cookies", this algorithm should return "I ekil seikooc"
for(c=each character in the string) {
if (c == NULL || c == 'space') {
while (!stackEmpty)) {
print(pop(stack));
}
}
if (c == NULL) {
break;
} else {
push(stack, c);
}
}