Format messages with paginated suffixes
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
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)
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
- The number of chunks is fixed up front: n = ceil(len(s) / w). Because the suffix is free, w only governs the content slices.
- Slice s in fixed strides of w: chunk k is s[k*w : (k+1)*w]. The final chunk may be shorter than w.
- Handle the empty-string case separately so it still emits exactly one line, " 1/1".
Format Messages with Paginated Suffixes (Suffix Counts Toward Width)
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
- 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.
- 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.
- Monotonicity: raising n never decreases reserved, so once content_width drops below 1 the problem is impossible — return [] immediately.