Welsh Custom Alphabet Sort
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reportedly served this Welsh Custom Alphabet Sort in February 2025, and the input size is the first thing to read. Up to 10^5 words and 2 * 10^5 total characters means you can't do anything clever per comparison that costs extra passes. It's a custom-comparator sort wearing a string costume. Tokenize each word once, map tokens to ranks, then sort on the rank lists. If the OA is tomorrow, this is a 15 minute problem once you see it. StealthCoder sits invisibly as a safety net if the tokenizing details slip when the clock is running.
The problem
Sort words using this Welsh alphabet of tokens: a, b, c, ch, d, dd, e, f, ff, g, ng, h, i, l, ll, m, n, o, p, ph, r, rh, s, t, th, u, w, y The tokens ch, dd, ff, ng, ll, ph, rh, and th are single alphabet symbols. Tokenize each word greedily using these two-letter symbols before single letters, then compare token ranks from left to right. If one token sequence is a prefix of another, the shorter word comes first. Function sortWelshWords(words: String[]) → String[] Examples Example 1 words = ["ddr","nah","dea","dd","ngah"] return = ["dea","dd","ddr","ngah","nah"] d precedes dd; dd is a prefix of ddr; and ng precedes n in the supplied token alphabet. Example 2 words = ["ca","cha","da","dda"] return = ["ca","cha","da","dda"] The leading tokens follow c < ch < d < dd. Constraints 1 <= words.length <= 10^5. Every word is nonempty, lowercase, and can be tokenized by the alphabet above. The total number of characters is at most 2 * 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop comparing strings and compare rank arrays. Build a map from each of the 29 tokens to its index in the given alphabet. Tokenize each word left to right: if the next two characters form one of ch, dd, ff, ng, ll, ph, rh, th, take both, otherwise take one. Store the rank list, then sort words by that list. Lexicographic list comparison already handles the prefix rule, so the shorter word comes first for free. Total tokenizing cost is linear in 2 * 10^5 characters, and the sort is O(n log n) comparisons. The common pitfall is re-tokenizing inside the comparator, which repeats work on every comparison. Another is forgetting that ng sits before h while n sits later, so ngah beats nah. If you blank on the live OA, StealthCoder is the hedge that reads the prompt and hands you the structure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Welsh Custom Alphabet Sort cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Welsh Custom Alphabet Sort FAQ
What's the trick in the Welsh Custom Alphabet Sort?+
Precompute a rank list per word using greedy two-letter-first tokenizing, then sort by those lists. Python compares lists lexicographically and treats a shorter prefix as smaller, so the prefix rule needs no extra code. Don't tokenize inside the comparator.
How hard is this Bloomberg question really?+
Easy to medium. There's no deep algorithm. The difficulty is careful tokenizing and knowing to cache ranks. Most failures come from sorting by raw characters or rebuilding tokens on every comparison and running slow on 10^5 words.
Why does greedy tokenizing work here?+
The two-letter symbols are fixed and the input is guaranteed tokenizable. At each position, check if the next two characters form a digraph and take it if so. Otherwise take one letter. Examples: ngah becomes ng, a, h, and dd stays one token.
What complexity should I aim for?+
Tokenizing is linear in total characters, at most 2 * 10^5. Sorting is O(n log n) comparisons over rank lists. Comparison cost is bounded by word length, and total length is capped, so this fits comfortably within the constraints.
How do I prepare for this in 48 hours?+
Write a custom-key sort a couple of times in your language. Practice a digraph tokenizer and test the two examples by hand, especially d before dd and ng before n. Edge cases: single-token words and one word being a prefix of another.