Where Does the Pattern First Appear in the Text?
Updated
Problem statement
Given a text and a pattern, find the earliest position in text where pattern appears as a run of consecutive letters, and return that position counting from 0. If pattern does not appear, return -1.
Compare letters yourself; the point of the exercise is to write the search.
Function
findSubstring(text: String, pattern: String) → intExamples
Example 1
text = "racecar"pattern = "ace"return = 1ace starts at index 1: the letters at positions 1, 2 and 3 of racecar are a, c and e.
Example 2
text = "banana"pattern = "nab"return = -1The text has n and a in many places, but the letters nab never come in that order, so the answer is -1.
Example 3
text = "aaaab"pattern = "aab"return = 2Try each start from the left. At index 0, aa match, but the third letter fails: the text has a where the pattern has b. Starting at 1 fails the same way. At index 2, all of aab matches, so that is the answer.
Example 4
text = "abababab"pattern = "bab"return = 1bab appears starting at index 1, and again at indices 3 and 5. The earliest start, 1, is the answer.
Constraints
1 <= text.length, pattern.length <= 5000textandpatternuse only lowercase English letters.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Count Binary SubstringsDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
- Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Validate a Word AbbreviationSalesforce · ONSITE INTERVIEWSalesforce interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences