Reported December 2022
Bloombergstring

Index of the First Non-Repeating Character

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The Bloomberg OA reported in December 2022 hides a trap inside a friendly-looking string problem. You need the index of the first character that appears exactly once, and the naive version breaks the moment every character repeats. Bloomberg asked it as "Index of the First Non-Repeating Character", and it's a counting problem with a string wrapper. It's easy if you stay calm and easy to botch if you rush the return value. If your head goes blank mid-assessment, StealthCoder runs invisibly as a safety net and puts the working solution on screen while you finish.

The problem

Given a non-empty string s, return the zero-based index of the first character whose total frequency in the string is exactly one.
If no such character exists, return -1. Character comparisons are case-sensitive.

Function
firstNonRepeatingIndex(s: String) → int

Examples
Example 1
s = "swiss"
return = 1
The character s repeats, while w occurs once at index 1.
Example 2
s = "aabbc"
return = 4
Only c has frequency one, and its index is 4.
Example 3
s = "aabb"
return = -1
Every character repeats, so there is no qualifying index.

Constraints
1 ≤ s.length ≤ 100000.
s contains printable ASCII characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two passes. First pass: count every character's frequency with a hash map or a fixed array of 128 slots, since the input is printable ASCII. Second pass: walk the string left to right and return the first index where the count is exactly one. Don't iterate the map for the answer, because map order isn't string order. The edge case that breaks naive solutions is the all-repeating string like "aabb", where you must return -1 after the loop ends, not fall off the end with an undefined value. Also remember comparisons are case-sensitive, so 'a' and 'A' are different keys. Don't lowercase anything. Another pitfall is nested loops that check each character against the rest. At length 100000 that's O(n^2) and risks a timeout. The two-pass approach is O(n) time and O(1) space. If you freeze on the OA, StealthCoder is the hedge that gets you unstuck.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Index of the First Non-Repeating Character 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as first unique character in a string. If you have time before the OA, drill that.

⏵ The honest play

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.

Index of the First Non-Repeating Character FAQ

How hard is the Bloomberg first non-repeating character question really?+

It's easy. The whole solution is a frequency count followed by one scan. The difficulty is in the details: returning -1 correctly, respecting case sensitivity, and avoiding an O(n^2) nested loop on a 100000-character input.

What's the trick to solving it fast?+

Count first, then scan in original order. Build frequencies in one pass, then loop through the string again and return the first index whose count is one. Never read the answer from the map, since its iteration order may not match string order.

Do I need a hash map or can I use an array?+

Either works. Since the input is printable ASCII, an integer array of size 128 is simpler and faster, with constant space. A hash map is fine too. Pick whichever you can write without bugs under pressure.

What edge cases should I test before submitting?+

Test "aabb" for the -1 case, a single-character string like "a" returning 0, and mixed case like "aA" where both are unique. Also test the unique character at the very end, as in "aabbc" returning 4.

How do I prepare for this in 48 hours?+

Write this solution from memory twice, once with an array and once with a map. Then do a few other counting problems like anagram checks. The pattern is frequency counting plus an ordered rescan, and it shows up constantly in string questions.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it