Citadel Securities Interview Question

Coding challenge was: Reverse the words in a sentence string in-place

Interview Answer

Anonymous

May 11, 2020

string = "This is a long sentence for test" # Create a list of all words in position but reversed using "[::-1]" list_reversed_string = [word[::-1] for word in string.split(" ")] reversed_string = " ".join(list_reversed_string) print(reversed_string) # ANOTHER SOLUTION string = "This is a long sentence for test" # Create a list of all words in position but reversed using "for loop" def reverse(word): w = "" for c in word: w = c + w return w list_reversed_string = [reverse(word) for word in string.split(" ")] reversed_string = " ".join(list_reversed_string) print(reversed_string)