Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Reverse Words Without Libraries
00:00
5 left

Reverse Words Without Libraries

MediumPython

Problem

Given a string, write a program to reverse the order of words without using built-in library functions. Treat any sequence of whitespace characters as a separator, discard leading and trailing whitespace, and return words separated by one space. Implement def reverse_words(s):, where s is a string and the result is a string. Do not use split(), join(), reverse(), or reversed().

Constraints

  • 0 <= len(s) <= 10^5
  • Words contain non-whitespace characters
  • Whitespace may appear between, before, or after words
  • The output must contain exactly one space between adjacent words
  • Do not use split(), join(), reverse(), or reversed()

Function Signature

def reverse_words(s):
Interviewer

Your question is Reverse Words Without Libraries. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.