Thursday, November 27, 2008

Loop in a linked list

How does one find a loop in a singly linked list in O(n) time using constant memory? you cannot modify the list in any way (and constant memory means the amount of memory required for the solution cannot be a function of n.)

Solution:

One way to detect a loop is to iterate over the list with 2 pointers at the same time where one is iterating at double speed. If the 2 pointers are ever equal after they iterate once and before they both reach an end, there's a loop.

Now for another puzzle.. I think the next question I had to answer was something along the lines of "OK, How do you remove a loop in a linked list with the same constraints?" The latter question definitely seems harder, but in the sequence of questions I'd already answered for the interviewer, the solution was pretty obvious. I'll leave that solution to someone else today.

Hm. I think I should pick up that book. I've always wondered where people got their juicy interview riddles.. I've always just repeated ones I've heard before or made up new ones along the same lines.

A better explaination to come soon

atoi

String manipulation functions are great programming questions. they test whether the user can understand and translate into code simple algorithms. string functions test pointer arithmetic which usually shows a knowledgeable programmer. also there are usually multiple solutions, some more efficient than others. plus people use them all the time so they should understand how they work. my favorite is atoi and i start the problem like this:

int atoi( char* pStr )

Write the definition for this function without using any built-in functions. if pStr is null, return 0. if pStr contains non-numeric characters, either return 0 (ok) or return the number derived so far (better) (e.g. if its "123A", then return 123). assume all numbers are positive. plus or minus signs can be considered non-numeric characters. in order to solve this program, the programmer must understand the difference between the integer 0 and the character '0', and how converting '0' to an int, will not result in 0. in other words, they have to understand what ascii is all about. if they are stuck solving this problem, just ask them first to write:

charToInt(char c)

if they can't do that then they basically missed half the problem. any moderately talented programmer who has a CS degree knows how to convert a char to an int. (note i said convert, not cast. charToInt('9') should return 9.)

when they start to solve the problem you will notice that they must make a choice in how they will process the string - from left to right or right to left. i will discuss both methods and the difficulties encountered in each.

"right to left" - this method starts at the right hand letter of the string and converts that character to an int. it then stores this value after promoting it to its correct "tens" place.

int atoi( char* pStr )
{
int iRetVal = 0;
int iTens = 1;

if ( pStr )
{
char* pCur = pStr;
while (*pCur)
pCur++;

pCur--;

while ( pCur >= pStr && *pCur <= '9' && *pCur >= '0' )
{
iRetVal += ((*pCur - '0') * iTens);
pCur--;
iTens *= 10;
}
}
return iRetVal;
}

"left to right" - this method keeps adding the number and multiplying the result by ten before continuing to the next number. e.g. if you had "6234" and you processed from left to right you'd have 6, then if you kept reading you'd multiply your result by 10 (6*10) to add a zero for where the next number would go. 60, and then you'd slide the 2 into the zero place you just made. 62. do it again, 620, slide the next number in, 623.

int atoi( char* pStr )
{
int iRetVal = 0;

if ( pStr )
{
while ( *pStr && *pStr <= '9' && *pStr >= '0' )
{
iRetVal = (iRetVal * 10) + (*pStr - '0');
pStr++;
}
}
return iRetVal;
}

i think the "left to right" method is a little bit cleaner, or maybe its just cooler. but both are "correct".

remember that debugging code on paper is somewhat hard. most programmers aren't used to studying code that much when you can just hit F-7, compile and see if the compiler barfs or not. if you notice an error, just ask them to step through a sample string drawing out what is happening with all the variables and the pointers in every step. they should find their mistake then and fix it (no points deducted).

Reverse a string word by word

problem: reverse "the house is blue", the answer should be "blue is house the". the words are reversed, but the letters are still in order (within the word).

solution: solving the initial problem of just reversing a string can either be a huge help or a frustrating hinderance. most likely the first attempt will be to solve it the same way, by swapping letters at the front of the string with letters at the back, and then adding some logic to keep the words in order. this attempt will lead to confusion pretty quickly.

for example, if we start by figuring out that "the" is 3 letters long and then try to put the "t" from "the" where the "l" from "blue" is, we encounter a problem. where do we put the "l" from "blue"? hmm... well we could have also figured out how long "blue" was and that would tell us where to put the "l" at... but the "e" from "blue" needs to go into the space after "the". argh. its getting quite confusing. in fact, i would be delighted to even see a solution to this problem using this attack method. i don't think its impossible, but i think it is so complex that it's not worth pursuing.

here's a hint. remember before when we just reversed "the house is blue"? what happened?

initial: the house is blue
reverse: eulb si esuoh eht

look at the result for a minute. notice anything? if you still don't see it, try this.

initial: the house is blue
reverse: eulb si esuoh eht
wanted : blue is house the
the solution can be attained by first reversing the string normally, and then just reversing each word.

Palindromes

Problem: this year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards).
10/02/2001
when was the last date that this occurred on? (see if you can do it in your head!)

solution: we know the year has to be less than 2001 since we already have the palindrome for 10/02. it can't be any year in 1900 because that would result in a day of 91. same for 1800 down to 1400. it could be a year in 1300 because that would be the 31st day. so whats the latest year in 1300 that would make a month? at first i thought it would be 1321, since that would give us the 12th month, but we have to remember that we want the maximum year in the 1300 century with a valid month, which would actually be 1390, since 09/31 is a valid date.

but of course, a question like this wouldn't be complete without an aha factor. and of course, there are not 31 days in september, only 30. so we have to go back to august 08 which means the correct date would be 08/31/1380.

palindromes also offer another great string question.
write a function that tests for palindromes
bool isPalindrome( char* pStr )

if you start a pointer at the beginning and the end of the string and keep comparing characters while moving the pointers closer together, you can test if the string is the same forwards and backwards. notice that the pointers only have to travel to the middle, not all the way to the other end (to reduce redundancy).

bool isPalindrome( char* pStr )
{
if ( pStr == NULL )
return false;

char* pEnd = pStr;
while ( *pEnd != '\0' )
pEnd++;

pEnd--;

while(pEnd > pStr)
{
if ( *pEnd != *pStr )
return false;

pEnd--;
pStr++;
}

return true;
}