FastPrepTop K N-gram Anagram Groups
Problems/Top K N-gram Anagram Groups

Top K N-gram Anagram Groups

Adobe logoAdobe● MediumINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The live coding task in this machine learning intern loop was reported as a blend of ideas: picking the k most frequent results over word n-grams, with anagram grouping and text chunking along the way. It came after resume questions in a single 75-minute panel and ran on HackerRank. Afterwards the panel asked about very large inputs, spoken-audio input, and how to judge the returned list.

The report does not give the exact rules, input format, limits or examples. Here the text is split into words, the n-grams overlap, n-grams with the same letters form one group, groups are ranked by count with ties broken by the group's alphabetically smallest member, which is also its name, n is 1 to 3, k is at most 100, and the text has at most 2 * 10^4 characters. The examples are our own. The follow-ups are covered in the editorial and are not judged.

The judged task matches the report at about 50%.

You are given a piece of text text, an n-gram size n and a number k.

  1. Split text into words. An n-gram is n consecutive words, written with single spaces. Take every n-gram, starting at every word position, so they overlap.
  2. Put two n-grams in the same anagram group when they use exactly the same letters the same number of times, ignoring spaces. For example, "listen to" and "silent to" are in one group, and so are "ab cd" and "cd ab".
  3. A group's count is the number of n-gram positions that belong to it. A group's name is its alphabetically smallest member.

Return the names of the k groups with the highest counts, highest first. Groups with equal counts are ordered by name, alphabetically. If there are fewer than k groups, return all of them.

Function

topAnagramGroups(text: String, n: int, k: int) → List<String>

Examples

Example 1

text = "god dog act cat dog god tac"n = 1k = 2return = ["dog","act"]

With n = 1 every word is its own n-gram. god and dog use the same letters, so they form one group with 4 occurrences. act, cat and tac form a group with 3. Each group is shown by its alphabetically smallest member: "dog", then "act".

Example 2

text = "listen to me silent to me"n = 2k = 2return = ["listen to","to me"]

The 2-grams are listen to, to me, me silent, silent to and to me. listen to and silent to use the same letters, so their group has 2 occurrences. to me also has 2, and me silent has 1. The two groups with 2 tie, so they are ordered by their names: "listen to" comes before "to me".

Example 3

text = "a b a"n = 3k = 5return = ["a b a"]

There is only one 3-gram, a b a, so only one group exists. k = 5 is more than the number of groups, so every group is returned.

Constraints

  • 1 <= text.length <= 2 * 10^4
  • text is words of 1 to 10 lowercase English letters separated by single spaces, with no leading or trailing space.
  • 1 <= n <= 3, and n is at most the number of words.
  • 1 <= k <= 100

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public List<String> topAnagramGroups(String text, int n, int k) {
    // Write your code here.
}
Checking account…