Sort Sentence Words
Updated
Problem statement
Given a string sentence containing lowercase English words separated by one or more ASCII spaces, return its words sorted by increasing length.
When two words have the same length, order them lexicographically. Return the ordered words as an array so the result does not depend on reconstructed whitespace.
Function
sortWords(sentence: String) → String[]Examples
Example 1
sentence = "banana fig apple kiwi"return = ["fig","kiwi","apple","banana"]The word lengths are 3, 4, 5, and 6, so no tie-breaker is needed.
Example 2
sentence = "pear plum fig apple"return = ["fig","pear","plum","apple"]pear and plum both have length 4, so their lexicographic order decides the tie.
Constraints
1 <= number of words <= 100000- The total number of non-space characters is at most
500000. - Every word contains only lowercase English letters.
- Adjacent words are separated by one or more ASCII spaces.
More Infosys Onsite Interview questions
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
- Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
- Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
- Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
- Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
- Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
- Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Sort Topic Names by Prefix and NumberPerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Sort Words by LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Check AnagramsDeloitte · ONSITE INTERVIEWDeloitte interview questions
- Check AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Check Anagram Pairs Index by IndexExpedia · ONSITE INTERVIEWExpedia interview questions
- Type-Ahead Prefix MatchesNavan · ONSITE INTERVIEWNavan interview questions