Count How Often a Word Appears in a Paragraph
Updated
Problem statement
Treat paragraph as a sequence of words, where a word is a maximal run of English letters. Spaces, punctuation, hyphens and digits all separate words.
Count how many of those words are equal to word, ignoring upper and lower case. A word only counts when the whole word matches, not when word appears inside a longer word.
Function
countWord(paragraph: String, word: String) → intExamples
Example 1
paragraph = "The cat sat. The cat, the CAT!"word = "cat"return = 3Case does not matter and punctuation is not part of a word, so cat, cat, and CAT! each count: 3.
Example 2
paragraph = "category cats scatter"word = "cat"return = 0Each of these words only contains cat inside it. A whole word has to match, so none of them counts and the answer is 0.
Example 3
paragraph = "go-goal 7go"word = "go"return = 2A hyphen or a digit ends a word. go-goal holds go and goal, and 7go holds the word go after the digit, so go appears twice; goal does not match.
Constraints
1 <= paragraph.length <= 10^5paragraphuses only English letters, digits, spaces and.,;:!?-.wordhas between1and20English letters.
More Capital One interview questions
- Count Records Present in A but Not BPHONE SCREEN · Seen Oct 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Maximum Subarray SumPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2025
- Ten-Pin Bowling Frame ScoresPHONE SCREEN · Seen Nov 2023
- Min StackPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2023
- Invert a Binary TreePHONE SCREEN · Seen Dec 2020
- In-Memory File System With Directory SizesPHONE SCREEN · ONSITE INTERVIEW
- Pick the K Nearest Grid Points to (0, 0)PHONE SCREEN · ONSITE INTERVIEW
- The Nth Number in the Fibonacci SequencePHONE SCREEN · ONSITE INTERVIEW
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- First Letter to Appear TwiceCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- Known Words Matching Phone DigitsLinkedIn · PHONE SCREENLinkedIn interview questions
- First Non-Repeated CharacterDell Technologies · PHONE SCREENDell Technologies interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- First Non-Repeating CharacterGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
Capital One interview 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
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)