InternDoor

Reverse the order of the words

Easystrings · two pointers

Reported in interviews at (where)

s holds words separated by spaces. A word is a run of characters that are not spaces. There may be extra spaces between words and at either end.

Return the words in reverse order, joined by single spaces, with no space at either end. The words themselves are not changed.

Example 1
Input
s = "interview prep starts today"
Output
"today starts prep interview"

Four words, last first.

Example 2
Input
s = " hello "
Output
"hello"

The spaces at both ends go.

Example 3
Input
s = "a b c"
Output
"c b a"

Two spaces between words become one.

Limits

  • 1 <= s.length <= 100000
  • s holds printable ASCII characters
  • s holds at least one word

Hints

Hint 1

Split the string into words, skipping runs of spaces, then join them from the last to the first. In a language with mutable strings you can also reverse the whole string and then reverse each word back.

Where this was reported

Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.

Run your code to check it on the examples. Submit runs every hidden test.

Ready for the real one?

Engineering internships and entry-level jobs in India, newest first.