Find the Index of the First Occurrence in a String
A easy-tier problem at 45% community acceptance, tagged with Two Pointers, String, String Matching. Reported in interviews at Pocket Gems and 18 others.
You're in an OA and hit a string matching problem. It looks simple: find the index where a needle first appears in a haystack. But 45% of candidates miss it. That's not because the algorithm is hard. It's because there's a trap in how you handle edge cases and what you optimize for. Apple, Google, and Microsoft have all asked this. If you blank on the implementation during your live assessment, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Find the Index of the First Occurrence in a String"
Find the Index of the First Occurrence in a String is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe naive approach is to iterate through the haystack and check if the substring matches at each position. That works. The real trick is handling the boundary case: if the needle is longer than the haystack, return -1 immediately. Most candidates skip this check and waste time on unnecessary iterations. Two Pointers or a rolling comparison both work fine here. The String Matching angle matters because some interviewers ask you to explain KMP or why brute force is acceptable for this constraint. If you freeze during the OA and can't remember the exact loop logic or edge case order, StealthCoder runs invisibly and gives you a clean, correct implementation to type. The acceptance rate reflects that many candidates either overthink it or stumble on the boundary logic under pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find the Index of the First Occurrence in a String recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the Index of the First Occurrence in a String interview FAQ
Is this problem actually asked at Google and Apple?+
Yes. It appears in the interview reports from both, alongside 17 other companies. It's a classic warm-up or middle-tier problem. Not a blocker, but frequent enough that you should know it cold before your OA.
Why is the acceptance rate so low if it's labeled EASY?+
Difficulty label doesn't match live OA performance. Candidates either overthink it, miss the edge case where needle > haystack, or stumble on the loop bounds under time pressure. The pattern is familiar but the implementation can trip you up.
Should I use Two Pointers or a built-in string method?+
Built-in methods (like indexOf or find) are often allowed and faster to code. If the role is senior or the platform forbids built-ins, implement Two Pointers yourself. The input topics list both, so either is acceptable depending on your company's rules.
What's the trick I'm missing if I keep failing test cases?+
Check your boundary conditions: empty needle, empty haystack, needle longer than haystack. Also verify your loop doesn't access out-of-bounds indices. These are the three biggest failure points in live OAs.
How does this relate to String Matching as a topic?+
It's the entry-level String Matching problem. Mastering this teaches you loop control and substring comparison logic. More advanced versions (KMP, Z-algorithm) build on this same foundation. It's a prerequisite pattern.
Want the actual problem statement? View "Find the Index of the First Occurrence in a String" on LeetCode →