Sort Words by Length
Updated
Problem statement
You are given a sentence: words made of English letters, separated by single spaces. Rearrange the words so that shorter words come first, and return the result as a sentence with single spaces between the words.
Words with the same number of letters must keep the order they had in the input. Upper-case and lower-case letters both count as one letter, and they do not affect the order.
Follow-up: how would you design this as a reusable class, for example one that can also sort by other rules later?
Function
sortWordsByLength(sentence: String) → StringExamples
Example 1
sentence = "the quick brown fox jumps"return = "the fox quick brown jumps"The lengths are the 3, quick 5, brown 5, fox 3, jumps 5. The two 3-letter words keep their order (the, then fox), and so do the three 5-letter words (quick, brown, jumps).
Example 2
sentence = "Keep calm and carry on"return = "on and Keep calm carry"Lengths: Keep 4, calm 4, and 3, carry 5, on 2. Shortest first: on, and, then Keep and calm in their original order, then carry. Upper-case letters count like any other letter.
Example 3
sentence = "single"return = "single"One word stays as it is.
Constraints
1 <= sentence.length <= 10^4sentenceconsists of English letters and single spaces, with no space at the start or end and never two spaces in a row.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
- Search a File CollectionGoogle · PHONE SCREENGoogle interview questions
- Versioned Recipe SystemThe Trade Desk · OA
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Predict Runtime Method DispatchAMD · ONSITE INTERVIEWAMD interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)