FastPrepCount Tag Occurrences
Problems/Count Tag Occurrences

Count Tag Occurrences

Datadog logoDatadog● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks to count each tag across an array of comma-separated tag strings. Case handling, whitespace and empty fields, repeated tags, output order, and bounds are specified for practice. The core task matches the report at about 85%.

Each string in records contains comma-separated tags. Count every occurrence of each tag across all records, including repeated appearances within one record.

Split on commas, trim ASCII spaces from both ends of each field, and ignore empty fields. Tags are case-sensitive. Return one string tag:count for each distinct tag, ordered lexicographically by tag. Counts use ordinary decimal digits with no spaces. Empty input or records containing only empty fields return an empty array.

Function

countTagOccurrences(records: String[]) → String[]

Examples

Example 1

records = ["tag1,tag2,tag1","tag2,tag3"]return = ["tag1:2","tag2:2","tag3:1"]

Across both records, tag1 occurs twice, tag2 twice, and tag3 once. Both occurrences of tag1 in the first record count.

Example 2

records = [" Team ,team,,Team",""]return = ["Team:2","team:1"]

Trim surrounding spaces and ignore empty fields. Tags are case-sensitive: Team occurs twice and team once. Uppercase T sorts before lowercase t.

Example 3

records = []return = []

With no records, there are no tag occurrences to report.

Constraints

  • 0 <= records.length <= 1000
  • 0 <= records[i].length <= 5000
  • The total number of characters in all records is at most 5000.
  • After trimming ASCII spaces, each comma-separated field is empty or contains 1 through 40 characters from A-Z, a-z, 0-9, _, and -.

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