FastPrepCount Repetitions for Each Word
Problems/Count Repetitions for Each Word

Count Repetitions for Each Word

Datadog logoDatadog● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for a separate repetition count for each word, with punctuation removal and lowercase normalization. Counting occurrences after the first, including zero-count words, token boundaries, output order and encoding, and bounds are specified for practice. The core task matches the report at about 80%.

Return a separate repetition count for every distinct word in paragraph. A word is a maximal consecutive run of English letters A-Z or a-z; all other characters separate words. Convert words to lowercase before comparing them.

For a word appearing c times, its repetition count is c - 1: the first occurrence is not a repetition. Include words with zero repetitions. Return strings word:repetitions in increasing lexicographic word order, using ordinary decimal counts and no spaces. Return an empty array when there are no words.

Function

wordRepetitionCounts(paragraph: String) → String[]

Examples

Example 1

paragraph = "Blue blue BLUE, red red. green"return = ["blue:2","green:0","red:1"]

The first occurrence of each word contributes zero. The two later blue occurrences contribute 2, the later red contributes 1, and the single green contributes 0. Return an entry for every distinct word in lexical order.

Example 2

paragraph = "zebra APPLE zebra apple"return = ["apple:1","zebra:1"]

Case-insensitive counting gives two occurrences of each word. Each word therefore has one occurrence after its first. Sort the two entries by word.

Example 3

paragraph = "123,!?"return = []

There are no English-letter words, so the result is empty.

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 String[] wordRepetitionCounts(String paragraph) {
    // Write your code here.
}
Checking account…