How do you write code on a whiteboard or a shared document without the safety net of an editor?
Write in larger blocks than you would on a screen. Indentation and punctuation become the main source of errors when there is no editor to catch them, so write whole functions rather than fragments and keep the structure visible on the page. Leave space to change your mind, because crossing out a block is harder than scrolling back.
def reverse_words(text):
words = text.split()
return " ".join(word[::-1] for word in words)
print(reverse_words("hello world"))
print(reverse_words("interview practice"))The outputs are olleh dlrow and weivretni ecircep, so each word is reversed while the order of the words is preserved.
The mechanism is that the cost of a whiteboard is not the algorithm but the transcription. Your usual flow is think, type, see the result, adjust, and on a whiteboard you lose the see and adjust steps, so you have to replace them with talking. Narrating the intent of each block, and saying the base case out loud before you write it, catches the errors that the compiler would otherwise catch for you.
The trade-off to name is that speed matters less than correctness here, and candidates often lose points by writing fast and messy. It is better to write one clean block and explain it than to produce three blocks of dense code. The other practical advice is to write the signature and the tests first, since those are the parts you can write without thinking, and they fix the shape of everything that follows.