Reported October 2022
Bloombergstring

Find the Index of the First Occurrence in a String

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 data structure here is just the string itself, and that's the whole joke. Bloomberg reported this one in October 2022: find the first index of needle in haystack, return -1 if it's missing, and an empty needle returns 0. It's the classic substring search, and it looks too easy to be an OA question. The catch is the 10^5 length limit on both strings. If you're taking this assessment soon, know the naive approach and the linear one. StealthCoder sits invisibly on your screen as a safety net if you blank mid-OA, but you won't need it if you read this first.

The problem

Return the zero-based index of the first occurrence of needle in haystack. Return -1 when no occurrence exists. An empty needle occurs at index 0.

Function
findFirstOccurrence(haystack: String, needle: String) → int

Examples
Example 1
haystack = "sadbutsad"
needle = "sad"
return = 0
The first match starts at index 0.

Constraints
Both strings contain at most 10^5 lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The naive method slides needle across haystack and compares character by character. That's O(n*m), and with both strings at 10^5 it can hit around 10^10 operations in the worst case, like haystack of all a's and needle of a's ending in b. The fix is KMP. Build the prefix function (failure table) for needle, then scan haystack once, never moving backward. That's O(n+m). Z-function or rolling hash also work, but hashing needs collision care. Handle the empty needle first and return 0 before anything else. Also check needle longer than haystack and return -1. The common pitfall is an off-by-one when you return the start index: it's i - m + 1, not i. If you blank on the failure table during the live Bloomberg OA, StealthCoder can supply the KMP code from the problem on screen.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Find the Index of the First Occurrence in a String 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find the index of the first occurrence 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find the Index of the First Occurrence in a String FAQ

How hard is this Bloomberg problem really?+

Easy on paper, but the 10^5 limits are the trap. A brute-force double loop can degrade to roughly 10^10 steps on adversarial input. If hidden tests include repeated characters, the naive version times out. Know KMP and you're safe.

What's the trick to solving it fast?+

Use KMP. Build the longest-prefix-suffix table for needle, then walk haystack once. When a mismatch happens, fall back in the table instead of restarting. That gives O(n+m) time and O(m) space.

What edge cases should I test?+

Empty needle returns 0. Needle longer than haystack returns -1. Needle equal to haystack returns 0. Repeated patterns like haystack aaaaab with needle aab. Also a match at the very end, where the start index math is easy to botch.

Can I just use the built-in find or indexOf?+

It might pass, but don't rely on it. Some assessments want you to write the search yourself, and behavior on an empty needle varies by language. Writing a clean manual version is safer and shows you understand the problem.

How do I prepare in 48 hours?+

Write KMP from memory twice. Practice building the prefix table until the fallback logic feels automatic. Then test it on aaaa, abab, and a no-match case. That's enough for this problem, and it carries over to other string matching 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