As reported: Wells Fargo Early Careers Global Technology internship assessment (HackerRank, three questions in 60 minutes: coding, database, programming/OOP; 2026). The coding question was about anagrams of the words in phrases.
You are given an array words of lowercase words and an array phrases of short sentences (lowercase, words separated by single spaces, no punctuation). Every word of every phrase appears in words.
A phrase can be changed by replacing each of its words with any word from words that is an anagram of it (including the word itself). For each phrase, return how many different phrases can be obtained this way.
Example 1
words = ["listen","silent","enlist","bat","tab","cat"]
phrases = ["bat cat listen", "tab silent"]
output = [6, 6]
bat has 2 choices (bat, tab), cat 1, listen 3 (listen, silent, enlist): 2 × 1 × 3 = 6. "tab silent": 2 × 3 = 6.
Example 2
words = ["the","bats","tabs","in","cat","act"]
phrases = ["cat the bats", "in the act", "act tabs in"]
output = [4, 2, 4]
Clarifications
- Choices are counted per position: the same anagram group appearing twice in a phrase multiplies twice.
- If
wordslists the same word more than once, each listing counts as a separate choice.
Constraints: 1 ≤ |words| ≤ 10^5; 1 ≤ |words[i]| ≤ 20; 1 ≤ |phrases| ≤ 1000; each phrase has 1–20 words; every answer is at most 10^15.
Code runner format: one JSON value per line: words, then phrases. Implement countPhrases(words, phrases) returning one count per phrase; the harness prints the list as JSON.