Problem · Array

Spam Text Classification

EasyIBMOA
See IBM hiring insights

You are given:

  • texts[n]: an array of text strings to classify
  • spamWords[k]: an array of words considered spam indicators

Classify each text as "spam" or "not_spam" using these rules:

  • A text is "spam" if it contains at least 2 occurrences of words that appear in spamWords.
  • A text is "not_spam" if it contains fewer than 2 such occurrences.
  • Matching is case-insensitive.
  • Words are separated by single spaces (treat each token as a word).
  • Each occurrence counts, including repeated spam words in the same text (for example, "paid paid" counts as 2).
  • Only consider words that exactly match a spam word (no partial matches).

Return an array of length n, where the ith value is the label for texts[i].

Examples
01 · Example 1
texts = ["free prize worth millions", "ten tips for a carefree lifestyle"]
spamWords = ["free", "money", "win", "millions"]
return = ["spam", "not_spam"]

The spam words found for each text are:

TextSpam Words FoundAnswer
free prize worth millionsfree, millionsspam
ten tips for a carefree lifestyle-not_spam

Return ["spam", "not_spam"].

More IBM problems
drafts saved locally
public String[] classifyTexts(String[] texts, String[] spamWords) {
  // write your code here
}
texts["free prize worth millions", "ten tips for a carefree lifestyle"]
spamWords["free", "money", "win", "millions"]
expected["spam", "not_spam"]
checking account