Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Encapsulation and Word Reversal

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

Your question is Encapsulation and Word Reversal. 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

Alarm alert messages may contain leading, trailing, or repeated whitespace. Implement an encapsulated function that returns the words in reverse order, separated by exactly one space. Word content, including punctuation, must remain unchanged.

A word is any maximal sequence of non-whitespace characters. The output must not contain leading or trailing whitespace. For an input containing only whitespace, return an empty string.

Formal Specification

  • Input: A string message containing zero or more printable characters and whitespace.
  • Output: A string containing the input words in reverse order, joined by one space.
  • The function must not modify the input string.

Constraints

  • 0 <= len(message) <= 10^5
  • Words may contain punctuation or digits
  • Whitespace may include spaces, tabs, and newline characters
  • The input string must not be modified

Function Signature

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