Sort Words by a Custom Alphabet
Updated
Problem statement
You are given a custom alphabet order and an array of lowercase words. Sort the words in dictionary order, but use the custom order for the letters.
To compare two words, find the first position where they differ. The word whose letter comes earlier in alphabet goes first. If one word is a prefix of the other (it matches the start of the other word), the shorter word comes first. Return the sorted words.
Function
sortWordsByAlphabet(alphabet: String, words: String[]) → String[]Examples
Example 1
alphabet = "zyxwvutsrqponmlkjihgfedcba"words = ["apple","app","banana"]return = ["banana","app","apple"]The alphabet is reversed, so b comes before a and banana goes first. The other two words both start with app, and app is a prefix of apple, so the shorter app comes next. The result is banana, app, apple.
Example 2
alphabet = "bacdefghijklmnopqrstuvwxyz"words = ["ab","aa","ba","b"]return = ["b","ba","ab","aa"]In this alphabet, b comes before a, so the words starting with b go first. Between b and ba, b is a prefix of ba, so b comes first. Between ab and aa, the second letters differ and b ranks before a, so ab comes before aa. The result is b, ba, ab, aa.
Example 3
alphabet = "abcdefghijklmnopqrstuvwxyz"words = []return = []An empty word array remains empty.
Constraints
alphabetcontains each of the 26 lowercase English letters exactly once.0 <= words.length <= 100000.- Every word is nonempty and contains only lowercase English letters.
- The total number of characters across
wordsis at most200000.
More Bloomberg LP Phone Screen questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Top Ten Most Frequent Words in a BookRobinhood · PHONE SCREENRobinhood interview questions
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Aggregate Matching IPv4 Addresses Across ResponsesPinterest · PHONE SCREENPinterest interview questions
- Diff Two Filesystem Hash TreesCursor · PHONE SCREENCursor interview questions
- Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
- Group AnagramsJFrog · PHONE SCREEN