Reverse Words In A String Problem Statement

Given a string of length N, reverse the string word by word. Ensure that the output reversed string has a single space between two words and does not contain leading or trailing spaces.

Example:

Input:
"  Welcome to   Coding  Ninjas"
Output:
"Ninjas Coding to Welcome"
Explanation:

The reversed string contains only a single space between words and eliminates any leading or trailing spaces.

Input:

The first line of input contains an integer T, representing the number of test cases. Each of the following T lines contains a string to be reversed word by word.

Output:

For each test case, return the reversed string with a single space between words and no trailing spaces.

Constraints:

  • 1 <= T <= 50
  • 0 <= N <= 10^5

Time Limit: 1 second

AnswerBot
2mo

Reverse the words in a string while ensuring single spaces and no leading or trailing spaces.

  • Input Handling: Read multiple test cases, each containing a string to be reversed word by word.

  • Trimming Spa...read more

Help your peers!
Select
Add answer anonymously...

Bajaj Finserv Software Developer interview questions & answers

A Software Developer was asked Q. Given two strings text1 and text2, return the length of their longest common sub...read more
A Software Developer was asked Q. Flood Fill Algorithm Task Assist Ninja in altering the color of a specific regio...read more
A Software Developer was asked Q. Alien Dictionary Problem Statement You are provided with a sorted dictionary (by...read more

Popular interview questions of Software Developer

A Software Developer was asked 2w agoQ1. Explain the internal workings of a hashmap.
A Software Developer was asked Q2. Given two strings text1 and text2, return the length of their longest common sub...read more
A Software Developer was asked Q3. Flood Fill Algorithm Task Assist Ninja in altering the color of a specific regio...read more
Bajaj Finserv Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits