FastPrepTop K Frequent Words
Problems/Top K Frequent Words

Top K Frequent Words

Rippling logoRippling● MediumINTERNNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports add document parsing/counting, most-common and top-three results, and valid-word filtering to top-K frequency selection. ASCII tokenization, lowercase normalization and an explicit allowlist complete unspecified parsing/filter conventions; the original lowercase-word/no-filter ranking is preserved in the second row. A two-row string matrix exposes total count and ranking together. Source match is about 90%.

You are given document text in documents. Count the valid words and return both the total number of valid word occurrences and up to k different words ranked by frequency. More frequent words come first. For equal counts, use alphabetical order.

Read each document separately. A token is a consecutive run of ASCII English letters, ending at any other character or the end of the document. Convert each token to lowercase. Every other ASCII character is a separator, and a token cannot continue into the next document.

If filterEnabled is false, count every token. Otherwise, count only tokens found in the supplied lowercase allowedWords list. Repeating a word in that list has no extra effect.

Return a string matrix with exactly two rows:

  • Row 0 contains one decimal string: the total number of valid token occurrences after lowercasing and filtering. Count repeated occurrences, not just different words.
  • Row 1 contains the ranked words. Return all available different valid words if there are fewer than k, or an empty row if there are none.

If no valid tokens remain, return [["0"],[]]. This two-row format returns both results.

Setting k=1 answers the most-common-word task, and k=3 answers the top-three task. An array of individual lowercase words with filtering disabled preserves the original top-K exercise. After building a solution that fully sorts the words, discuss using a bounded heap when k is much smaller than the number of different words.

Function

topKFrequentWords(documents: String[], k: int, filterEnabled: boolean, allowedWords: String[]) → String[][]

Examples

Example 1

documents = ["red","blue","red","green","blue","red"]k = 2filterEnabled = falseallowedWords = []return = [["6"],["red","blue"]]

The original word-array case remains unchanged: red appears 3 times and blue 2.

Example 2

documents = ["pear","apple","pear","apple","plum"]k = 2filterEnabled = falseallowedWords = []return = [["5"],["apple","pear"]]

Equal counts retain lexical tie order.

Example 3

documents = ["go","go","go"]k = 1filterEnabled = falseallowedWords = []return = [["3"],["go"]]

k=1 returns the single most common word.

Example 4

documents = ["red, RED blue.","green red blue"]k = 3filterEnabled = trueallowedWords = ["blue","green"]return = [["3"],["blue","green"]]

Normalize and filter before counting. Blue occurs twice and green once; red is excluded.

Constraints

  • 1 <= documents.length <= 100000; documents contain ASCII characters and total at most 200000 characters.
  • Every maximal letter token has length at most 30; a document may be empty or contain only delimiters.
  • 1 <= k <= 100000.
  • 0 <= allowedWords.length <= 100000; each entry has 1..30 lowercase English letters, with total allowlist characters at most 200000.

More Rippling Phone Screen questions

  • Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
  • Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
  • Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
  • Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
  • Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
  • Group AnagramsWorkday · PHONE SCREEN
  • Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
  • Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
  • Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
  • Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
  • Top Ten Most Frequent Words in a BookRobinhood · PHONE SCREENRobinhood interview questions
  • Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions

Interview reports that mention this question

  • Rippling Intern Interview History: 17 Experiences (Part 5 of 5)
  • Rippling Intern Onsite Interviews: 18 Experiences (Part 4 of 5)
  • Rippling SWE Intern Interviews: 101 Experiences (Part 1 of 5)

Rippling interview experiences

  • Rippling Senior SWE Full Loop Questions: 1 Experience
  • Rippling Experienced SWE Phone Screens: 2 Experiences
  • Rippling Design Rounds and Onsites: 17 Experiences (Part 3 of 3)
All Rippling interview questionsSee Rippling hiring insights
Saved
class Solution {
    public String[][] topKFrequentWords(String[] documents, int k, boolean filterEnabled, String[] allowedWords) {
        // Write your code here.
    }

}
Checking account…