Quote:
Originally Posted by dmmik
Here is a whiteboard C coding question I was asked at an actual game company interview. Complete this function:
Code:
bool IsPalendrome(char* str)
{
}
|
Apologies for thread resurrection.
I saw this last night while browsing, and it's a nice little problem, if not especially taxing. As a novice, I originally thought it would be best to break it down into an array and do something complicated such as reversing the order of the array and then comparing them. But there is no need - after further reading, having seen the string tools available. Once I was thinking along those lines, it took about 20 mins.
So here's a solution (in python rather than C) - anyone got any optimisation tips?
Code:
#!/usr/bin/python
def isPalindrome(string):
length = len(string)
#print length
i=0
j=length
while j > 0:
if string[i] == string[j-1]:
#print string[i],string[j-1]
i=i+1
j=j-1
else:
print 'Not a palindrome'
break
if j == 0:
print 'Word is a palindrome'
else:
pass
input = raw_input()
isPalindrome(input)
The mild advantage of this is that it's not typed, so works equally well with palindromic numbers. For the c problem, I guess you'd have to put in some type checking to sanitise the input before doing anything.