Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Words in a String

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

Your question is Reverse Words in a String. 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

A MITRE ATT&CK content utility needs to display the words in a procedure description in reverse order. Given a string text, return a string containing the same words in reverse order, separated by exactly one space.

Leading, trailing, and repeated whitespace must be removed from the result. A word is any maximal sequence of non-whitespace characters. Do not use split(), reversed(), or slicing to reverse the text.

Formal Specification

Implement reverse_attack_words(text).

  • Input: text, a Python string.
  • Output: A Python string with words in reverse order and normalized spaces.

Constraints

  • 0 <= len(text) <= 100,000
  • A word is a maximal sequence of non-whitespace characters
  • Whitespace can include spaces, tabs, and newlines
  • Do not use split(), reversed(), or slicing to reverse the full text

Function Signature

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