Truncated Push Notifications

A gym's mobile app shows push notifications on the lock screen, but the preview only fits 50 characters. Anything longer gets cut off, so the marketing team wants to find and rewrite those messages.

Return notification_id and body_length (the number of characters in body) for every notification whose body is longer than 50 characters. A body of exactly 50 characters fits and must not be returned. Count characters, not bytes: an accented letter such as é counts as one. Rows may be in any order; return no rows if every message fits.

Tables

push_notifications — One row per push notification drafted for the gym app.

ColumnTypeDescription
notification_id (key)INTEGERUnique notification id
categoryTEXTMessage type: 'promo', 'reminder' or 'account'
bodyTEXTFull message text, may contain accented letters

Example

push_notifications

notification_idcategorybody
1promoFlash sale: 20% off all yoga mats today only
2reminderYour spin class with Coach Dana starts in 30 minutes. Grab a towel!
3promoSpin class moved to Studio B this Saturday morning
4accountPayment received. Thanks!
5reminderCafé closes early on Friday: pick up your protein shakes before 4pm
6accountYour membership renews in 7 days. Tap to review it.

Expected output

notification_idbody_length
267
567
651

Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.

SQL Path · Select & Filter · problem 6 of 70 · SQLite

You need a function that measures a string, not the string itself.
In SQLite, LENGTH on a text value returns the number of characters.
"Longer than 50" excludes exactly 50.

The idea

LENGTH(body) returns how many characters a text value has, so the filter is WHERE LENGTH(body) > 50, and the same expression in the SELECT list, aliased as body_length, produces the second column. Strict > matters: a 50-character message fits the preview exactly.

For text, SQLite counts characters. Letters such as é or ñ take two bytes in UTF-8, so a byte count, for example LENGTH(CAST(body AS BLOB)), overstates their length. Character count is what the lock screen cares about.

The alternative avoids counting altogether: SUBSTR(body, 51) is everything from the 51st character on. It is non-empty exactly when the body has more than 50 characters. This trick is useful when you only need to know whether a string passes a limit.

Solution

SELECT notification_id,
       LENGTH(body) AS body_length
FROM push_notifications
WHERE LENGTH(body) > 50;

Another way

SELECT notification_id,
       LENGTH(body) AS body_length
FROM push_notifications
WHERE SUBSTR(body, 51) <> '';

Why common answers fail

Uses >= 50, so messages of exactly 50 characters, which do fit, are flagged too.

SELECT notification_id, LENGTH(body) AS body_length FROM push_notifications WHERE LENGTH(body) >= 50;

Measures bytes instead of characters, so text with accented letters looks longer than it is.

SELECT notification_id, LENGTH(CAST(body AS BLOB)) AS body_length FROM push_notifications WHERE LENGTH(CAST(body AS BLOB)) > 50;

Compares the text itself with the string 50 alphabetically instead of comparing its length.

SELECT notification_id, LENGTH(body) AS body_length FROM push_notifications WHERE body > '50';

What the interviewer is checking

Checks basic string functions, strict versus inclusive boundaries, and awareness of characters versus bytes.

Mistakes to avoid

  • Using >= when the limit itself is allowed.
  • Counting bytes instead of characters for non-English text.
  • Comparing the text to a number instead of its length.

Follow-ups to expect

  • Show the 50-character preview the user actually sees. (SUBSTR(body, 1, 50))
  • Find the longest message per category. (MAX(LENGTH(body)) with GROUP BY category)
  • Ignore leading and trailing spaces when measuring. (LENGTH(TRIM(body)))

In real work

Apps validate SMS, push, meta-description and ad-copy lengths against display limits before content goes live.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url