Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Sentence With Word Order

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

Your question is Reverse Sentence With Word Order. 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

An applied AI annotation workflows may receive sentences with inconsistent spacing before they are displayed or evaluated. Given a string, return the sentence with its words in reverse order, separated by exactly one space.

A word is any maximal sequence of non-whitespace characters. Leading, trailing, and repeated whitespace must not appear in the result. Preserve every character within each word, including punctuation, digits, and symbols.

Formal Specification

Implement reverse_sentence(s), where s is a string. Return a string containing the words from s in reverse order. If s contains no words, return the empty string.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable characters and whitespace
  • A word is a maximal sequence of non-whitespace characters
  • Word characters must be preserved exactly

Function Signature

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