PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithm design and string-processing skills, focusing on constraint-aware segmentation with variable-length suffix metadata and the accompanying complexity analysis.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Segment a message with width-constrained suffixes

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a message string s and an integer width W, split s into consecutive segments and append a suffix "i/n" to each segment indicating its 1-indexed position i out of the total n segments. Part A (suffix excluded from width): Split s into chunks of up to W characters (not counting the suffix), then append the suffix to each line; return the list of lines. Part B (suffix included in width): Now len(chunk) + len(suffix(i,n)) must be ≤ W for every line, but n is unknown in advance because the suffix length depends on n. Produce valid lines that cover the entire message in order (breaking anywhere is allowed; no reordering). If impossible (e.g., W < len("1/1")), return an error. Implement an efficient algorithm to determine n and the segmentation and analyze its complexity.

Quick Answer: This question evaluates algorithm design and string-processing skills, focusing on constraint-aware segmentation with variable-length suffix metadata and the accompanying complexity analysis.

Segment Message with Suffix Excluded from Width

Split a message into chunks of at most W characters, then append i/n to each chunk.

Constraints

  • W counts only message characters in this part

Examples

Input: ('abcdefghij', 4)

Expected Output: ['abcd1/3', 'efgh2/3', 'ij3/3']

Explanation: Three chunks before suffixes.

Input: ('', 5)

Expected Output: ['1/1']

Explanation: Empty message still produces one empty segment with suffix.

Input: ('abc', 0)

Expected Output: []

Explanation: Impossible width returns empty list.

Hints

  1. n is known after chunking by W.

Segment Message with Suffix Included in Width

Split a message so every segment plus its i/n suffix has length at most W, choosing the smallest feasible n.

Constraints

  • Breaking anywhere is allowed
  • Return [] when impossible

Examples

Input: ('abcdefghij', 6)

Expected Output: ['abc1/4', 'def2/4', 'ghi3/4', 'j4/4']

Explanation: Suffix consumes part of width.

Input: ('abc', 3)

Expected Output: []

Explanation: Only one-character payload fits with 1/3 suffixes.

Input: ('abc', 2)

Expected Output: []

Explanation: Width too small for any suffix.

Input: ('', 3)

Expected Output: []

Explanation: Empty message with one suffix.

Hints

  1. Try increasing n until total payload capacity reaches the message length.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)