FastPrepFastPrep
Problem Brief

Prepare Notification

FULLTIMEOA
See Microsoft online assessment and hiring insights

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:

  • the notification should be as long as possible;
  • only whole words can be cropped;
  • if any words are cropped, the notification should end with "…"; the dots should be separated from the last word by a single space;
  • the notification may not exceed the K-character limit, including the dots.
  • If all the words need to be cropped, the notification is "..." (three dots without a space).

    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.
  • 1Example 1

    Input
    message = "And now here is my secret", K = 15
    Output
    "And now..."
    Explanation
  • the notification "And ..." would be incorrect, because there is a longer correct notification
  • the notification "And now her ..." would be incorrect, because the original message is cropped through the middle of a word
  • the notification "And now ..." would be incorrect, because it ends with a space
  • the notification "And now here..." would be incorrect, because it ends with a space
  • the notification "And now here..." would be incorrect, because there is no space before the three dots
  • the notification "And now here ..." would be incorrect, because it exceeds the 15-character limit
  • The function should return "And now...", as explained above.

    2Example 2

    Input
    message = "There is an animal with four legs", K = 15
    Output
    "There is an ..."
    Explanation
    The function should return "There is an...".

    3Example 3

    Input
    message = "super dog", K = 4
    Output
    "..."
    Explanation
    The function should return "...".

    4Example 4

    Input
    message = "how are you", K = 20
    Output
    "how are you"
    Explanation
    The function should return "how are you".

    Constraints

    Limits and guarantees your solution can rely on.

  • 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 spaces
  • message does not contain spaces at the beginning or at the end
  • words are separated by a single space (there are never two or more consecutive spaces)
  • public String prepareNotification(String message, int K) {
        // write your code here
    }
    
    Input

    message

    "And now here is my secret"

    K

    15

    Output

    "And now..."

    Sign in to submit your solution.