FastPrepCount a Character in a String
Problems/Count a Character in a String

Count a Character in a String

Capital One logoCapital One● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2021 Capital One report from a software engineer candidate lists a short counting task first: report how often one chosen character occurs in a piece of text.

The report does not say whether case matters or which characters can appear. Here the match is exact, so uppercase and lowercase are different, and the text uses letters, digits, spaces and a few punctuation marks, up to 10^5 characters. These are our choices.

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

Return how many positions of the string s hold exactly the character c. The match is case-sensitive: 'A' and 'a' are different characters.

Function

countCharacter(s: String, c: char) → int

Examples

Example 1

s = "Capital Card"c = "a"return = 3

Lowercase a shows up in Capital twice and in Card once, for a total of 3.

Example 2

s = "Capital card Co"c = "C"return = 2

Uppercase C starts Capital and Co, so the count is 2. The lowercase c at the start of card is a different character and is not counted.

Example 3

s = "2026-11-17"c = "7"return = 1

Digits count like any other character: 7 appears once, at the very end.

Constraints

  • 1 <= s.length <= 10^5
  • s and c use only English letters, digits, spaces, ., , and -.

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

  • Christmas Tree with N TiersVerily · ONSITE INTERVIEW
  • Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
  • 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
  • Implement tail -nConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Valid ParenthesesEPAM · ONSITE INTERVIEWEPAM interview questions
  • Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir 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 countCharacter(String s, char c) {
        // Write your code here.
    }
}
Checking account…