Format Text
Updated
Problem statement
Given multiple paragraphs, their alignment (LEFT or RIGHT), and a maximum line width, format the text like a newspaper: pack words greedily into lines, pad spaces according to alignment, and add a border of * around the entire output.
Rules:
- For each paragraph, pack as many words as possible into each line without exceeding
width(greedy, using two pointers). - If alignment is
LEFT: pad trailing spaces so each line is exactlywidthcharacters. - If alignment is
RIGHT: pad leading spaces so each line is exactlywidthcharacters. - Wrap all lines with a
*border: one*on each side of every content line, plus a full row of*characters on the top and bottom. The total line width (including border characters) iswidth + 2.
Return the formatted output as a list of strings.
Function
formatText(paragraphs: String[], alignments: String[], width: int) → List<String>Examples
Example 1
paragraphs = ["abc"]alignments = ["LEFT"]width = 3return = ["*****","*abc*","*****"]width=3, border line = "*****" (width+2=5 stars). The single word "abc" fills the line exactly. With LEFT alignment no extra padding is needed. Output: top border, content line "*abc*", bottom border.
Example 2
paragraphs = ["hello world"]alignments = ["LEFT"]width = 7return = ["*********","*hello *","*world *","*********"]width=7, border line = "*********" (9 stars). "hello" (5) + " world" would exceed 7, so they wrap to separate lines. Each is LEFT-padded with trailing spaces to fill 7 chars: "hello " and "world ". Content lines become "*hello *" and "*world *".
More TikTok interview questions
- Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
- Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 2026
- Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
- Shell Command Counts with History ReferencesOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Sep 2026
- Product Minus Sum of DigitsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Count Case-Insensitive TripletsOA · Seen Aug 2026
Similar questions at other companies
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions
- Concatenate Digit-wise SumsZipRecruiter · OAZipRecruiter interview questions
- Reverse First K Characters in Every 2K BlockArcesium · OAArcesium interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Thresholded Run-Length EncodingBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Tennis Game ScoreAbridge · OAAbridge interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
TikTok interview experiences
- TikTok AI Infra Intern LLM Round 2: 1 Experience
- TikTok AI Infra Intern DeepSpeed Round: 1 Experience
- TikTok AI Infra Intern Kernel Round: 1 Experience
- TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
- TikTok AI Infra Intern Interview Timeline: 1 Experience
- TikTok AI Infra Intern Onsite Round 2: 1 Experience