FastPrepSort Words by Length
Problems/Sort Words by Length

Sort Words by Length

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a review of a Microsoft software engineering internship. The task as reported is one listed question, framed as building a system. Its core is putting a sentence's words in order of how long they are. The review also covers an online assessment and a pair of later technical rounds. By its account, class design got somewhat more weight than usual in those rounds, alongside ordinary algorithm work. It does not place this question in either round.

It does not say how ties are broken, what the words contain, or how the answer is returned. Here words are separated by single spaces and use only English letters, shorter words come first, words of equal length keep their original order, and you return the reordered sentence. The class-design part is a follow-up discussed in the editorial and is not tested.

Our version is about 80% the same as the reported question.

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) → String

Examples

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^4
  • sentence consists 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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public String sortWordsByLength(String sentence) {
        // Write your code here.
    }
}
Checking account…