Sort the Words
Updated
Problem statement
Split sentence into words separated by one or more ASCII spaces. Return those words in increasing lexicographic order, preserving repeated words.
Ignore leading and trailing spaces. An empty sentence or a sentence containing only spaces produces an empty array. Words contain lowercase ASCII letters, so a shorter prefix comes before the longer word.
Function
sortWords(sentence: String) → String[]Examples
Example 1
sentence = "pear apple pear"return = ["apple","pear","pear"]Apple precedes pear, and the two pear occurrences are retained.
Example 2
sentence = " app apple a "return = ["a","app","apple"]The shorter prefix sorts before a longer word; surrounding and repeated spaces do not create words.
Constraints
0 <= sentence.length <= 10000sentence contains only lowercase ASCII letters a through z and ASCII spaces.Each nonempty word has length at most 50.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
- Flat JSON Value DifferencesChubb · OAChubb interview questions
- Lexicographic Word FrequenciesUpstart · OAUpstart interview questions
- Email Log Processing, Grouping, and SortingStripe · OAStripe interview questions
- Most Frequent CharacterMicrosoft · OAMicrosoft interview questions
- Get Maximum Information GainAmazon · OA
- Better CompressionGoldman Sachs · OAGoldman Sachs interview questions
- Process LogsSnowflake · OASnowflake interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)