Java · Coding problem
Notification Preview
A free coding problem from the Java: Numbers, Bits and Strings skill. Read it here, then solve it in the app in Java.
- Easy
- Coding problem
- Free
- Java
The problem
A chat app in Dublin shows a short preview of each new message in the phone's notification. The preview is at most n characters long:
- If the message has
ncharacters or fewer, show the whole message. - Otherwise show its first
ncharacters followed by....
A "character" here means what the reader sees as one symbol, so Hi 👋 has 4 characters. Messages can contain emoji, and an emoji must never be cut in half. (The tests use single-code-point emoji only, with no skin tones, flags or joined emoji. The deep dive explains those.)
Example
Input: n = 10, message = See you at 6 in Austin
Output: See you at...
Input: n on the first line, the message on the second. Output: the preview.
Constraints
1 <= n <= 1000- the message is 1 to 1000 characters, with no leading or trailing spaces
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Java: Numbers, Bits and Strings
Solve it Notification Preview free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime