Reverse words in a log message

Python data engineering interview problem. Difficulty: beginner. Pattern: Strings. About 10 minutes. Free to practice.

Ops pastes a one-line log message into a ticket. For a postmortem slide they want the words in reverse order while leaving each word itself readable.

A log message is sometimes easier to scan when the last word comes first. Reverse the order of the words in a message. The letters inside each word stay in order, and words are joined with a single space. Write reverse_words(message). 'retry failed job' becomes 'job failed retry'. A single word like 'one' stays 'one'. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Reverse the order of the words.
  • Keep the letters of each word as they are.
  • Join the words with one space.

Constraints

  • message is a string.
  • Words are separated by whitespace.

Examples

Input: reverse_words('retry failed job') Output: 'job failed retry' This input follows the stated rules and produces this output.

Topics: lakebench, python, strings.

More Python interview questions · All interview problems · Learn data engineering

beginner

Reverse words in a log message

Interview-style drill: Reverse the order of words in a log message while keeping punctuation attached to its word.

Reverse the order of words in a log message. Do not reverse the letters inside a word.