Rearrange Words in a Sentence

You are provided with a sentence 'TEXT'. Each word in the sentence is separated by a single space, and the sentence starts with a capital letter. Your task is to rearrange the words in 'TEXT' based on increasing order of their length.

Example:

Input:
T = 1
TEXT = "Hello World from OpenAI"
Output:
"AI from World Hello Open"
Explanation:

The words are rearranged based on their lengths: "AI" (2), "from" (4), "World" (5), "Hello" (5), "Open" (4). Since "Hello" and "World" have the same length, they are arranged based on their original order.

Constraints:

  • 1 <= T <= 5
  • 1 <= |TEXT| <= 105
  • Where | 'TEXT' | denotes the length of the 'TEXT' sentence.
Note:
If two words have the same length, they remain in their original order as in 'TEXT'. 
AnswerBot
4mo

Rearrange words in a sentence based on increasing order of their length.

  • Split the sentence into words and calculate the length of each word.

  • Sort the words based on their lengths in increasing order.

  • If...read more

Help your peers!
Select
Add answer anonymously...

JPMorgan Chase & Co. Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Count Subarrays with Given XOR Problem Statement You are given an array of integ...read more
A Software Developer Intern was asked Q. Kth Largest Number Problem Statement You are given a continuous stream of number...read more
A Software Developer Intern was asked Q. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Count Subarrays with Given XOR Problem Statement You are given an array of integ...read more
A Software Developer Intern was asked Q2. Kth Largest Number Problem Statement You are given a continuous stream of number...read more
A Software Developer Intern was asked Q3. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more
JPMorgan Chase & Co. Software Developer Intern 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