FastPrepCount All Occurrences of Duplicate Words
Problems/Count All Occurrences of Duplicate Words

Count All Occurrences of Duplicate Words

Datadog logoDatadog● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives a paragraph where the word I appears twice and the answer is 2. This exercise preserves that example by counting all occurrences of repeated words. The general counting rule, token boundaries, case handling, empty input, and bounds are clarified for practice. The core task matches the report at about 85%.

Count word occurrences that belong to a repeated word group in paragraph. A word occurring c times contributes c when c >= 2, and contributes zero when it occurs once.

A word is a maximal consecutive run of English letters A-Z or a-z; other characters separate words. Compare words without regard to case. Include the first occurrence of every repeated word in the total. Return zero when there are no repeated groups, including when there are no words.

Function

countDuplicateWordOccurrences(paragraph: String) → int

Examples

Example 1

paragraph = "This is a nice car, I would like to buy one. But I don't have enough money"return = 2

Only i appears more than once. Both of its occurrences count, so return 2.

Example 2

paragraph = "Blue blue BLUE, red red. green"return = 5

The repeated groups are blue with three occurrences and red with two. Count all five occurrences, including each group's first occurrence. green occurs once and contributes zero.

Example 3

paragraph = "a b c"return = 0

Every word appears exactly once. There are no occurrences belonging to a repeated group.

Constraints

  • 0 <= paragraph.length <= 10000
  • Every character is printable ASCII, a tab, a carriage return, or a line feed.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public int countDuplicateWordOccurrences(String paragraph) {
    // Write your code here.
}
Checking account…