Word Break: Construct One Sentence
Updated
Problem statement
Insert spaces into s to construct a sentence whose words all belong to wordDict. Preserve every character in its original order, use exactly one space between neighboring words, and include no leading or trailing space. Dictionary words may be reused.
Return one complete sentence. For this exercise, assume that when several sentences are possible, return the lexicographically smallest sentence, comparing the resulting strings with the space character ordered before lowercase letters. If no complete sentence exists, return the empty string.
Function
wordBreakSentence(s: String, wordDict: String[]) → StringExamples
Example 1
s = "catsanddogs"wordDict = ["cat","sand","cats","dog","and","dogs"]return = "cat sand dogs"Both cat sand dogs and cats and dogs are valid. The space after cat sorts before the next letter in cats, so cat sand dogs is selected.
Example 2
s = "abcd"wordDict = ["a","ab","cd"]return = "ab cd"Choosing a immediately would leave an impossible suffix. The complete sentence is ab cd.
Example 3
s = "applepenapple"wordDict = ["apple","pen"]return = "apple pen apple"The dictionary word apple is reused, producing apple pen apple.
Example 4
s = "catsandog"wordDict = ["cats","dog","sand","and","cat"]return = ""No dictionary segmentation covers all characters, so the result is empty.
Constraints
- For this exercise, assume
1 <= s.length <= 300,1 <= wordDict.length <= 1000, and1 <= wordDict[i].length <= 20. sand dictionary words contain only lowercase English letters. Dictionary words are unique.
More Meta Onsite Interview questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Word BreakBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest String ChainVisa · OAVisa interview questions
- Minimum Dictionary SegmentsGoogle · PHONE SCREENGoogle interview questions
- Count Equal-Frequency SubsequencesExpedia · OAExpedia interview questions
- Longest String ChainLinkedIn · OALinkedIn interview questions
- Can the Text Split Into Dictionary WordsAdobe · PHONE SCREENAdobe interview questions
- Longest Common URL History RunDatadog · PHONE SCREENDatadog interview questions
- Word Break IIWex · PHONE SCREEN
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences