Crop a Notification at Whole-Word Boundaries
Updated
Problem statement
A message contains words separated by one space. Return a notification containing at most K characters.
- If the entire message fits, return it unchanged.
- Otherwise remove whole words from its end. Keep as many complete words as possible from the beginning and append
...: one space followed by three ASCII dots. - If no word can remain, return
...with no preceding space.
The dots and their separating space count toward the limit. Never split a word or leave a trailing space.
Function
solution(message: String, K: int) → StringExamples
Example 1
message = "And now here is my secret"K = 15return = "And now ..."A longer prefix would exceed the limit after including the space and dots.
Example 2
message = "alpha beta"K = 10return = "alpha beta"The complete message fits exactly, so no ellipsis is added.
Example 3
message = "longword x"K = 7return = "..."The first word plus a space and three dots would exceed seven characters.
Constraints
- For this exercise, assume
1 <= message.length <= 100000and3 <= K <= 100000. - For this exercise, assume words contain English letters, with exactly one space between words and no leading or trailing spaces.
More Marshall Wace Online Assessment questions
- Count Letters with Ordered Lowercase and Uppercase OccurrencesOA · Seen Oct 2025
- Maximum Trailing Zeros on a Path with at Most One TurnOA · Seen Oct 2025
- Shortest Subarray Covering an Integer RangeOA · Seen Oct 2025
- Maximum Events in a 60-Second WindowOA · Seen Aug 2025
- Successful Logins from Multiple CountriesOA · Seen Aug 2025
- Maximize Remaining Item Types After Clearing Consecutive ShelvesOA · Seen Nov 2024
- Shortest String After Erasing Equal PairsOA · Seen Nov 2024
- Count Distinct Tags in a Fixed-Radius Circle (Practice Reconstruction)OA · Seen Apr 2024
- Generate Unique Company Email AddressesOA · Seen Apr 2024
- Reconstruct a Two-Row Binary MatrixOA · Seen Apr 2024
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- First Non-Repeating CharacterAmazon · OA · ONSITE INTERVIEW
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Count Palindrome Addition ChoicesThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Count Palindrome Addition ChoicesHudson River Trading · OAHudson River Trading interview questions
- Count Phrases Formed by Anagram ReplacementWells Fargo · OAWells Fargo interview questions
- Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions