FastPrepWhere Does the Pattern First Appear in the Text?
Problems/Where Does the Pattern First Appear in the Text?

Where Does the Pattern First Appear in the Text?

Capital One logoCapital One● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A November 2013 Capital One intern report from Chicago, describing a final round of case, behavioral and technical interviews, says the technical question was to find one string inside another.

No format or limits are given. Here the text and the pattern are lowercase strings of 1 to 5000 letters, the answer is the index of the pattern's first appearance, and -1 means it does not appear. The statement asks for the comparison to be written by hand rather than with a library search. These are our choices.

Our version is about 85% the same as the reported question.

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) → int

Examples

Example 1

text = "racecar"pattern = "ace"return = 1

ace 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 = -1

The 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 = 2

Try 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 = 1

bab 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 <= 5000
  • text and pattern use 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
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int findSubstring(String text, String pattern) {
        // Write your code here.
    }
}
Checking account…