PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-processing and algorithm-design skills, including handling variable-length metadata, preserving character order, managing boundary cases, and formal time/space complexity reasoning.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Format messages with paginated suffixes

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You are given a message string s and an integer width w. Split s into multiple chunks, preserving character order, and append a suffix "i/n" to each chunk where i is the 1-based index and n is the total number of chunks. Part 1: Assume the suffix does not count toward width; output all chunked strings with their suffixes. Part 2 (hard): The suffix must count toward width and n is unknown in advance because the suffix length depends on n. Design an algorithm to determine n and produce the chunks so that each chunk (content + a single space + "i/n") fits within w characters. Handle edge cases (e.g., when w is too small to fit even "1/1"), analyze time and space complexity, and justify correctness.

Quick Answer: This question evaluates string-processing and algorithm-design skills, including handling variable-length metadata, preserving character order, managing boundary cases, and formal time/space complexity reasoning.

Format Messages with Paginated Suffixes (Suffix Free of Width)

You are given a message string `s` and an integer width `w`. Split `s` into chunks of at most `w` characters, preserving character order, then append a suffix ` i/n` to each chunk, where `i` is the 1-based chunk index and `n` is the total number of chunks. Each returned line is `content + " " + "i/n"`. In this part the suffix does NOT count toward the width budget `w` — i.e. you chunk `s` purely by `w` content characters and then tack the suffix on afterward. Return the list of formatted lines. Rules / edge cases: - `n = ceil(len(s) / w)`; if `s` is empty, treat it as a single empty chunk so `n = 1` and the line is `" 1/1"`. - If `w <= 0`, return an empty list (no valid chunking is possible). Example: `s = "helloworld"`, `w = 5` → `["hello 1/2", "world 2/2"]`.

Constraints

  • 0 <= len(s)
  • w may be any integer; w <= 0 yields an empty result
  • Characters are kept in their original order across chunks
  • The suffix " i/n" is appended AFTER chunking and does not consume width

Examples

Input: ("abcdefgh", 3)

Expected Output: ['abc 1/3', 'def 2/3', 'gh 3/3']

Explanation: len 8, w 3 -> n = ceil(8/3) = 3 chunks 'abc','def','gh'; last chunk is short.

Input: ("helloworld", 5)

Expected Output: ['hello 1/2', 'world 2/2']

Explanation: len 10, w 5 -> exactly 2 full chunks.

Input: ("abc", 5)

Expected Output: ['abc 1/1']

Explanation: Whole string fits one chunk -> n = 1.

Input: ("", 4)

Expected Output: [' 1/1']

Explanation: Empty input is a single empty chunk; line is the space + suffix only.

Input: ("abcdefghij", 1)

Expected Output: ['a 1/10', 'b 2/10', 'c 3/10', 'd 4/10', 'e 5/10', 'f 6/10', 'g 7/10', 'h 8/10', 'i 9/10', 'j 10/10']

Explanation: w = 1 forces ten single-char chunks; n is two digits in the suffix.

Input: ("xy", 0)

Expected Output: []

Explanation: w <= 0 cannot chunk anything -> empty result.

Hints

  1. The number of chunks is fixed up front: n = ceil(len(s) / w). Because the suffix is free, w only governs the content slices.
  2. Slice s in fixed strides of w: chunk k is s[k*w : (k+1)*w]. The final chunk may be shorter than w.
  3. Handle the empty-string case separately so it still emits exactly one line, " 1/1".

Format Messages with Paginated Suffixes (Suffix Counts Toward Width)

Same setup as the previous part, but now the suffix ` i/n` MUST count toward the width: each emitted line `content + " " + "i/n"` must have total length at most `w`. The hard part is that `n` is unknown in advance — the suffix length depends on the number of digits in `n` (and `i`), which depends on `n` itself. Design an algorithm that determines `n` and produces chunks so that every line fits within `w`. Approach: search for the smallest feasible `n`. For a candidate `n` reserve a uniform worst-case suffix budget of `1 (space) + digits(n) + 1 (slash) + digits(n)` characters (the worst suffix is ` n/n`). The per-chunk content budget is then `content_width = w - reserved`. A candidate `n` works when `content_width >= 1` and `content_width * n >= len(s)`. Because increasing `n` never shrinks `reserved`, once `content_width < 1` the task is impossible. Return the list of formatted lines, or an empty list if `w` is too small to fit even "1/1" plus any required content. Edge cases: - If `s` is empty, one line `" 1/1"` is emitted when `w >= 4`, else `[]`. - When `w` is too small (e.g. `w = 3` with non-empty `s`), return `[]`. Example: `s = "abcdefgh"`, `w = 10` → reserved for n=2 is `1+1+1+1 = 4`, content_width 6, two chunks → `["abcdef 1/2", "gh 2/2"]` (each length <= 10).

Constraints

  • 0 <= len(s)
  • Every emitted line content + " " + "i/n" must have length <= w
  • n is not given; it must be derived so the suffix (whose length grows with digits(n)) still fits
  • Return [] when no n can satisfy the width (e.g. w too small for "1/1" plus content)

Examples

Input: ("abcdefgh", 10)

Expected Output: ['abcdef 1/2', 'gh 2/2']

Explanation: n=1 reserves 4 -> content 6, 6*1=6 < 8 fails; n=2 reserves 4 -> content 6, 6*2=12 >= 8 works. Each line <= 10.

Input: ("hello", 7)

Expected Output: ['hel 1/2', 'lo 2/2']

Explanation: n=1: content 3, 3<5 fails; n=2: content 3, 3*2=6>=5 works -> chunks of 3 then 2. Lines 'hel 1/2' (7) and 'lo 2/2' (6).

Input: ("abc", 8)

Expected Output: ['abc 1/1']

Explanation: n=1 reserves 4 -> content 4 >= 3, single chunk fits; line length 7 <= 8.

Input: ("", 4)

Expected Output: [' 1/1']

Explanation: Empty message, w=4 exactly fits ' 1/1' (length 4).

Input: ("abc", 3)

Expected Output: []

Explanation: n=1 reserves 4 > 3 so content_width < 1 immediately -> impossible, return [].

Input: ("x", 6)

Expected Output: ['x 1/1']

Explanation: n=1 reserves 4 -> content 2 >= 1, single char fits; line 'x 1/1' length 5 <= 6.

Hints

  1. The suffix length is not fixed: it depends on digits(n). Reserve a uniform worst-case budget of 1 + digits(n) + 1 + digits(n) so the content boundary is the same for every chunk.
  2. For a candidate n, content_width = w - reserved. The candidate is feasible iff content_width >= 1 and content_width * n >= len(s). Search for the smallest such n.
  3. Monotonicity: raising n never decreases reserved, so once content_width drops below 1 the problem is impossible — return [] immediately.
Last updated: Jun 25, 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
  • 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)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)