Problem 06 of 75

Reverse Words in a String

MediumNot solved yetInterview me on this
Before you code · which pattern fits?

The problem

Given an input string s, reverse the order of the words. A word is a maximal substring of non-space characters. Return a string with the words in reverse order joined by a single space, with no leading or trailing spaces.

"the sky is blue" → "blue is sky the" " hello world " → "world hello"

Your solution runs against 3 unit tests, including edge and large-value cases.

Loading...

Test console · 3 unit tests

Submit your code to run it against all 3 unit tests. Results appear here.

JavaScript, TypeScript & Python run sandboxed in your browser; other languages run on the execution server. Your code is saved locally as you type.

Hint

Reference solution

Video explanation

Reverse Words in a String, explainedKnowledge Center