FastPrepPhrase Covering the Most Words
Problems/Phrase Covering the Most Words

Phrase Covering the Most Words

Zip logoZip● MediumFULLTIMENEW GRADPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report preserves contiguous phrase examples and explicitly describes the score as trie depth times occurrence count. It records uncertainty about converter-call optimization and omits tie rules; the judged phrase-scoring task matches the usable source at about 86%.

Given an array of space-separated sentences, choose one non-empty contiguous phrase that appears in one or more sentences.

A phrase's coverage score is:

number of words in the phrase × number of distinct sentences containing the phrase.

Return the phrase with the greatest coverage score. If several phrases tie, prefer the phrase with more words. If a tie remains, return the lexicographically smallest phrase.

Function

mostSharedPhrase(sentences: String[]) → String

Examples

Example 1

sentences = ["I feel very good","I feel very bad","hello","hello world"]return = "I feel very"

The phrase has three words and appears in two sentences, so its score is 3 × 2 = 6. No other phrase scores more.

Example 2

sentences = ["a b","a c"]return = "a b"

a scores 1 × 2 = 2. Each two-word sentence also scores 2 × 1 = 2, so the longer phrase wins; a b is lexicographically smaller than a c.

Example 3

sentences = ["one two three"]return = "one two three"

With one sentence, the entire sentence has the maximum possible word count and score.

Constraints

  • 1 <= sentences.length <= 100.
  • Each sentence contains between 1 and 50 non-empty words separated by one space.
  • Words contain printable ASCII characters other than spaces.
  • The total number of words is at most 500.

More Zip problems

  • Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
  • Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
  • Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
All Zip interview questionsSee Zip hiring insights
Saved
class Solution {
    public String mostSharedPhrase(String[] sentences) {
        // Write your code here.
    }
}
Checking account…