Goat Latin (LeetCode 824): Clean String Building
Goat Latin transforms each word of a sentence: words starting with a vowel get 'ma' appended, other words move their first letter to the end and then get 'ma'. Finally the i-th word (1-indexed) gets i copies of 'a'.
#Straightforward, but watch the details
def to_goat_latin(sentence):
vowels = set('aeiouAEIOU')
out = []
for i, w in enumerate(sentence.split(), 1):
if w[0] not in vowels:
w = w[1:] + w[0]
out.append(w + 'ma' + 'a' * i)
return ' '.join(out)
print(to_goat_latin('I speak Goat Latin'))
Imaa peaksmaaa oatGmaaaa atinLmaaaaa
#What interviewers look for
Easy string problems are about precision. Call out uppercase vowels, the 1-based count of a's, and joining with single spaces. The total output length grows quadratically with the number of words because of the a suffixes, so the runtime is O(n + k^2) for k words; saying that unprompted is a nice signal.
Disclosure: I'm on the Linkjob AI team; we make an AI interview copilot (https://www.linkjob.ai/interview-copilot/) for practicing answers like this before real interviews.