Thu 3 Apr 2008
reversing a string is a classic tech interview question. of course you’re not allowed to call any built-in reverse() function. A solution might look like, in my pseudocode:
someString = new string("732432424");
char temp;
int end=someString.length()-1;
for (int x=0;x<end;x++)
{
temp=someString[end];
someString[x]=someString[end];
someString[end]=temp;
end--;
}
but sometimes the interviewer asks you to reverse the string in place, without using other memory, and for that, we’ll need our XOR friend
A B XOR ----+--- 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0
someString = new string("732432424");
char temp;
int end=someString.length()-1; //assume zero based char arrays
for (int x=0;x<end;x++)
{
someString[x]=someString[x] XOR someString[end];
// first loop: someString[x]=3
someString[end]=someString[end] XOR someString[x];
//first loop: someString[end]=7
someString[x] = someString[x] XOR someString[end];
//first loop: someString[x]=4
end--;
}
April 3rd, 2008 at 11:13 am
Your XOR truth table looks suspiciously like an OR truth table.
April 14th, 2008 at 9:58 am
good catch. thx. i corrected the table. it’s XOR now!