Character Intersection and Frequency Ordering
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive solution here is tie-breaking, and Bloomberg candidates reported this one in January 2022. Two strings come in, and you return two strings: the shared characters in first-appearance order, then every distinct character sorted by combined frequency. It looks like a quick hash-table job. It is. But the ordering rules trip people who reach for a plain sort or a set. If you blank on the tie-break during the OA, StealthCoder runs invisibly on your screen as a safety net and hands you the structure.
The problem
Return a two-element array: A string containing each distinct character present in both inputs, in first-appearance order from first. A string containing every distinct character from both inputs ordered by descending combined frequency. Break ties by first appearance while scanning first then second. Function intersectionAndFrequencyOrder(first: String, second: String) → String[] Examples Example 1 first = "ABCEGDB" second = "ABACE" return = ["ABCE","ABCEGD"] A and B occur three times, C and E twice, and G and D once; ties follow first appearance. Constraints Inputs contain ASCII characters and total length is at most 2 * 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build one frequency map across both strings, and record first-appearance index while scanning first, then second. That index is your tie-breaker. For the intersection, make a set from second, then walk first and keep each character once if it's in that set. Use a seen set so duplicates don't repeat. For the frequency string, collect distinct characters and sort by (negative count, first index). Check your example by hand: A and B hit 3, C and E hit 2, G and D hit 1. The pitfall is using an unordered map and assuming iteration order, or sorting with an unstable comparator that ignores the index. Another trap is counting only one string's frequency. With 2 * 10^5 total length, O(n log k) is fine, since k is bounded by the ASCII range. StealthCoder is your hedge if the comparator logic slips mid-assessment.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Character Intersection and Frequency Ordering 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Character Intersection and Frequency Ordering FAQ
What's the trick to the Bloomberg character intersection problem?+
Track two things per character: combined count across both strings and the index of first appearance, scanning first then second. Sort by count descending, then by that index. Everything else is a set lookup for the intersection.
How hard is this one really?+
Easy to medium. The algorithm is a hash map and a sort. The difficulty is in the details: tie-breaking order, deduplication, and counting both strings. Most failures come from skipped edge cases, not from the concept.
What edge cases should I test?+
Test empty strings, no shared characters, a character repeated many times in only one input, and ties across the two strings. Also test a character that first appears in second only. Its index must come after everything in first.
Do I need a sort, or can I avoid it?+
Since inputs are ASCII, there are at most 128 or 256 distinct characters. A sort on that small list is effectively constant time. You could also bucket by frequency, but a comparator sort is simpler and less error-prone.
How do I prepare for this in 48 hours?+
Write it once from scratch in your language. Use a map for counts, a map for first index, and a sort with a two-key comparator. Run the sample by hand, then add two edge cases. Frequency-with-ordering questions like this come up repeatedly.