Problem · String
Prepare Notification
Learn this problemProblem statement
Prepare a notification of the given message which will be displayed on a mobile device. The message is made of words separated by single spaces. The length of the notification is limited to K characters. If the message is too long to be displayed fully, some words from the end of the message should be cropped, keeping in mind that:
Functin Description:
Complete function prepareNotification in the editor.
Function prepareNotification has the following parameters:
String message & int k
Returns:
The notification to display, which has no more than K chars, as described above.Function
prepareNotification(message: String, K: int) → StringExamples
Example 1
message = "And now here is my secret"K = 15return = "And now ...""And now ..." is the longest notification that keeps whole words, includes the required space before the dots, and stays within 15 characters. "And now here ..." would be 16 characters long.
Example 2
message = "There is an animal with four legs"K = 15return = "There is an ...""There is an ..." is exactly 15 characters long, including the space and the three dots.
Example 3
message = "super dog"K = 4return = "..."The function should return "...".
Example 4
message = "how are you"K = 20return = "how are you"The function should return "how are you".
Constraints
K is an integer within the range [3..500]the length of string message is within the range [1..500]string message is made of English letters (a-z, 'A-Z) and spacesmessage does not contain spaces at the beginning or at the endwords are separated by a single space (there are never two or more consecutive spaces)More Microsoft problems
- Maximum Pipeline ThroughputOA · Seen Jul 2026
- Maximum Strong Team SubarrayOA · Seen Jul 2026
- Minimum Cost K-Capable ModelsOA · Seen Jul 2026
- Alphabetically Smallest PalindromeOA · Seen Jul 2026
- Maximum Reward PointsOA · Seen Jul 2026
- Maximum Strength of Every NeuronOA · Seen Jul 2026
- Neural Network Subnetwork StrengthOA · Seen Jul 2026
- XOR MultiplicationOA · Seen Jul 2026