Prepare Notification
Updated
Problem statement
You are given a message whose words are separated by single spaces and an integer limit K. Return the notification to display using at most K characters.
If the entire message fits, return it unchanged. Otherwise, remove one or more whole words from the end. Keep as many leading words as possible and append the three ASCII dots .... When at least one word remains, place exactly one space before the dots. If no word can remain, return ... without a leading space. The dots and any preceding space count toward the limit.
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 and fits within 15 characters. Keeping the next word would produce And now here ..., which has 16 characters.
Example 2
message = "There is an animal with four legs"K = 15return = "There is an ..."There is an ... has exactly 15 characters, including the space and the three dots.
Example 3
message = "super dog"K = 4return = "..."The word super cannot fit together with the required suffix, so all words are removed and the result is ....
Example 4
message = "how are you"K = 20return = "how are you"The complete message already fits within 20 characters, so it is returned unchanged.
Constraints
3 <= K <= 5001 <= message.length <= 500messagecontains only English letters and spaces.messagehas no leading or trailing spaces.- Adjacent words are separated by exactly one space.
More Microsoft interview questions
- Maximum Score with Prime JumpsOA · Seen Dec 2026
- Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Authentication SystemOA · Seen Nov 2026
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
- Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
- Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
- Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase interview questions
- Missing WordsVanguard · OAVanguard interview questions
- Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
- Smallest String After at Most One DeletionLSEG · OALSEG interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)