Microsoft Interview Question
Software Engineer in Tests# Program:
def reversal(string):
if string is None:
return string
if len(string) == 1 or 0:
return string
string = string.encode('utf-8')
string = list(string)
i = 0
j = len(string) - 1
while i <= j:
string[i], string[j] = string[j], string[i]
i += 1
j -= 1
return ''.join(string)
print reversal('ab csdjkf,!wiuro ')
# Test cases:
a) Single character string
b) Empty string
c) Null string
d) Unicode character string
e) Lengthy string (50 MB)
f) String with spaces, punctuations, exclamation, period, etc,.
This seems to be a very common question - to reverse a string
- acoader November 05, 2008I'd give strings of size 0,1,2,3, and some very large length string, and NULL for testing. To reverse, use 2 pointers one moving from left to right and the other from right to left, and keep swapping the contents pointed by them. Do this while the distance between them <= 1.