Split an Outbound SMS Into Billable Carrier Segments

Last updated: November 29, 2026

Quick Overview

Given a message body, decide whether it can be sent in GSM-7 or must fall back to UCS-2, then split it into the concatenated SMS segments a carrier would bill for, without ever splitting a GSM escape pair or a UTF-16 surrogate pair across segments.

Twilio
Coding & Algorithms
Software Engineer
Twilio
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a message body, decide whether it can be sent in GSM-7 or must fall back to UCS-2, then split it into the concatenated SMS segments a carrier would bill for, without ever splitting a GSM escape pair or a UTF-16 surrogate pair across segments.

Segment count drives what a Twilio Programmable Messaging customer is billed, so a backend screen on the messaging team might use this to see whether a candidate can turn a fiddly telecom spec into clean code. The interviewer checks that you ask about encodings before coding, handle the single versus concatenated limits correctly, and respect multi-unit characters at segment boundaries.

What the Interviewer Expects
  • Asks which characters are in the GSM-7 basic set versus the extension table before writing code
  • Knows the limits: 160/153 septets for GSM-7 and 70/67 code units for UCS-2, with the smaller numbers reserved for the concatenation header
  • Charges extension characters such as the euro sign or braces 2 septets and emoji 2 UTF-16 code units
  • Never splits a two-unit character across a segment boundary
  • Runs in a single linear pass and handles the empty string
  • Explains why one stray emoji can more than double a customer's bill
Key Topics to Cover
String processing
Character encoding
GSM-7 and UCS-2
Greedy segmentation
Billing correctness
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add a smart-encoding option that replaces curly quotes and similar characters with GSM-7 lookalikes before choosing the encoding?
  • Return only the segment count and cost for a 1 MB batch of messages without building the segment strings. What changes?
  • How would you support national language shift tables (for example Turkish or Spanish) that change which characters are single-septet?
  • Where in a sending pipeline would you compute this, and how would you surface it to customers before they send?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a Unicode string. Output: the encoding chosen and the list of segments.

  • If every character is in the GSM-7 basic or extension table, use GSM-7. Basic characters cost 1 septet; extension ...
High-Level Design
  1. Pick the encoding with one scan over the string.
  2. Choose a cost function and the single/multi limits for that encoding.
  3. Sum the costs. If within the single limit, return the whole text.
  4. Oth...

Submit Your Answer
Markdown supported

Related Questions