Count SMS Segments Under GSM-7 and UCS-2 Encoding Rules
Last updated: November 29, 2026
Quick Overview
Given a message body, decide whether it can be encoded in GSM-7 or must fall back to UCS-2, then return how many SMS segments it will be billed as, accounting for extended characters and characters that cannot be split across segments.
Attentive
November 29, 20260
0
0 solved
Given a message body, decide whether it can be encoded in GSM-7 or must fall back to UCS-2, then return how many SMS segments it will be billed as, accounting for extended characters and characters that cannot be split across segments.
Carriers bill per segment, and one emoji can turn a 150 character promo into a three-segment UCS-2 message. A product like Attentive's message composer has to show an accurate segment count as a marketer types. The question checks careful rule modeling, not a clever algorithm.
What the Interviewer Expects
- Detect the encoding: GSM-7 only if every character is in the basic or extended table
- Charge extended GSM characters (such as the euro sign and braces) two septets
- Use 160/153 limits for GSM-7 and 70/67 for UCS-2, explaining the header overhead
- Never split an escape pair or a UTF-16 surrogate pair across segments
- Handle the empty message and the exact-boundary lengths
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you suggest replacements (smart quotes to straight quotes) to keep a message in GSM-7?
- How does a personalized merge field like the first name change the estimate?
- How would you make the count incremental as the user types?
- How do MMS or RCS change the cost model?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Return (encoding, segments). GSM-7 holds 160 septets in a single SMS. Once a message is concatenated, each part carries a user data header, which leaves 153 septets. UCS-2 holds 70 UTF-16 units, or ...
High-Level Design
First, pick the encoding with one pass over the text. Second, map each character to its cost. If the total fits the single-segment limit, the answer is 1. Otherwise, pack greedily: start a new segment...