Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse a Sentence Word Wise

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reverse a Sentence Word Wise. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Novigo Solutions processes short text commands that must be displayed with their word order reversed. Given a sentence, return a new sentence whose words appear from last to first.

A word is any maximal sequence of non-whitespace characters. Preserve each word exactly, including punctuation attached to it, but return the result with exactly one space between adjacent words and no leading or trailing spaces.

Formal Specification

Implement reverse_sentence(sentence), where sentence is a string. Return a string containing the words of sentence in reverse order. Any whitespace characters, including spaces, tabs, and newlines, separate words and are normalized in the output. The input contains at least one word.

Constraints

  • 1 <= len(sentence) <= 10^5
  • The input contains at least one non-whitespace character
  • Words may contain punctuation
  • Whitespace may include spaces, tabs, and newlines

Function Signature

def reverse_sentence(sentence):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output