Alphabetize a Word List
Updated
Problem statement
You are given a list of words, words. Return the same words in alphabetical order, the way a dictionary lists them.
To decide which of two words comes first:
- Compare them letter by letter from the left. At the first position where they differ, the word with the earlier letter in the alphabet comes first.
- If one word runs out of letters first and every letter so far matched, the shorter word comes first. For example,
"car"comes before"cart".
Keep repeated words. If a word appears 3 times in words, it appears 3 times in the answer.
Function
alphabetize(words: String[]) → String[]Examples
Example 1
words = ["pear","fig","apple","date"]return = ["apple","date","fig","pear"]Compare first letters: a < d < f < p. So the order is apple, date, fig, pear.
Example 2
words = ["cart","car","cat","ca"]return = ["ca","car","cart","cat"]All four words start with ca. "ca" ends first, so it comes first. "car" is the start of "cart", so it comes before it. "cat" comes last because its third letter t is after r.
Example 3
words = ["kiwi","lime","kiwi"]return = ["kiwi","kiwi","lime"]Repeated words stay in the list. Both copies of "kiwi" come before "lime".
Constraints
1 <= words.length <= 20001 <= words[i].length <= 10- Every
words[i]contains only lowercase English letters.
More DocuSign Online Assessment questions
- Max Sum With No Two Adjacent PicksOA · Seen Apr 2026
- Max Sum With No Two Adjacent Picks on a CircleOA · Seen Apr 2026
- Beautiful Number OrderingsOA · Seen Feb 2026
- Grid Path With the Most ClearanceOA · Seen May 2025
- Vehicle Mixes for a Wheel TotalOA · Seen May 2025
- Count Sales Rung Up at the Wrong PriceOA · Seen Feb 2021
- Most Disjoint Intervals From Starts and DurationsOA · Seen Feb 2021
- Fewest Fountains to Water the GardenOA · Seen Feb 2021
- Lowest Start That Keeps Running Totals PositiveOA · Seen Feb 2021
- Fewest Student Groups Within a Level GapOA · Seen Nov 2020
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
- Find a File in a Directory SnapshotCresta · OACresta interview questions
- Flat JSON Value DifferencesChubb · OAChubb interview questions
- Road Trip (Analyst Program :PBlackRock · OABlackRock interview questions
- Get Search ResultsJP Morgan Chase · OAJP Morgan Chase interview questions
- Lexicographic Word FrequenciesUpstart · OAUpstart interview questions
- Email Log Processing, Grouping, and SortingStripe · OAStripe interview questions
- Most Frequent CharacterMicrosoft · OAMicrosoft interview questions