Tuesday, February 22, 2011

Quickie: Binary Printing

This one is probably about the right size for a phone screen question. I've seen this as print a binary string, print a decimal string, and print a hex string. They are all good and it shouldn't take someone more than a few minutes of thinking and a few minutes of scribbling to get it done.



public static String printme(int i) {
StringBuilder mOut = new StringBuilder();

while(true) {
if(i % 2 == 1) {
mOut.insert(0, "1");
} else {
mOut.insert(0, "0");
}
i = i / 2;
if(i == 0) {
return mOut.toString();
}
}

}





For the reader: code up an integer representation without using ten else-ifs or java's automatic integer to string converters. Just append chars.

Quickie: String Reversing

An oldie but a goodie (actually, not a goodie, but I'll talk about that at the end). This question is often phrased in two parts:


First: Write an algorithm to reverse a string.


This should be pretty boring to you. Just start at the ends of the array and swap each element until the ends touch. I find keeping a start and an end counter to be easier to code on a whiteboard. Some people like to just increment a single counter for the start and calculate the end pointer from the length of the array. Probably a slight performance hit that way, but I wouldn't no hire someone for it.


int mStart = 0;
int mEnd = mChars.length - 1;
while(mStart < mEnd) {
char mTemp = mChars[mStart];
mChars[mStart] = mChars[mEnd];
mChars[mEnd] = mTemp;
mStart++;
mEnd--;
}



So the second part of the question is: now reverse the order of the words in the string without reversing the words themselves so that a string "this is a test" would convert to "test a is this".


I'm not a fan of this for two reasons: First, half the candidates have seen this an know the trick. Second, for those who haven't seen it, it requires a flash of insight. Anything that requires a flash of insight just tests whether the candidate is lucky enough to have insightful flashes durring interviews. I'd rather see something test coding skill, algorithmic reasoning, grasp of recursing, anything like that. Just not luck.


But whatever, someone is probably going to ask you this question someday so here is the answer: reverse the string once, then reverse each word again. So first you make "tset a si siht" then you reverse that to "test a is this".


int mStart = 0;
int mEnd = mChars.length - 1;
while(mStart < mEnd) {
char mTemp = mChars[mStart];
mChars[mStart] = mChars[mEnd];
mChars[mEnd] = mTemp;
mStart++;
mEnd--;
}

int mLastWordStart = 0;
int mLastWordEnd = 0;
int mCurrentLocation = 0;
while(mCurrentLocation < mChars.length) {

mLastWordStart = -1;
mLastWordEnd = -1;
while((mCurrentLocation < mChars.length) &&
(mChars[mCurrentLocation] == ' ')) {
mCurrentLocation++;
}

mLastWordStart = mCurrentLocation;

while((mCurrentLocation < mChars.length) &&
(mChars[mCurrentLocation] != ' ')) {
mCurrentLocation++;
}

mLastWordEnd = mCurrentLocation - 1;

while(mLastWordEnd > mLastWordStart) {
char mTemp = mChars[mLastWordEnd];
mChars[mLastWordEnd] = mChars[mLastWordStart];
mChars[mLastWordStart] = mTemp;
mLastWordEnd--;
mLastWordStart++;
}

}




There it is! Variations of this that I've seen include a question about how to flip a bitmapped image which is represented as a two dimensional array of pixels. The solution (which I'll leave largely to the reader) is treat each line of the file as a string to reverse and reverse them once. You could also probably do some cool pattern based conditional flipping by combining the word-by-word string reversal with the bitmap question.