Format a Centered Newspaper Page
Updated
Problem statement
You are given an array of paragraphs paragraphs and an integer width. Each paragraph is a list of words in order.
Lay out the paragraphs one after another. Inside a paragraph, keep the words in order and put one space between neighboring words. Fill each line with as many of the next words as fit, as long as the line is not longer than width. Then start a new line. A new paragraph always starts on a new line.
Center each line in a space exactly width characters wide. If a line needs s extra spaces, put floor(s / 2) of them before the text and the rest after it. So when s is odd, the extra space goes on the right.
Add a newspaper border made of * characters. The first and last strings you return are rows of width + 2 asterisks. Every text line gets one asterisk at the start and one at the end.
Return the whole bordered page as an array of strings.
Function
formatCenteredNewspaperPage(paragraphs: String[][], width: int) → String[]Examples
Example 1
paragraphs = [["The","quick","brown","fox"],["jumps","over","it"]]width = 10return = ["************","*The quick *","*brown fox *","*jumps over*","* it *","************"]The first paragraph fills two lines, The quick and brown fox. Each is one character short, so its one extra space goes on the right. In the second paragraph, jumps over fills its line exactly. Then it is centered with four spaces on each side. The top and bottom borders each have 12 asterisks.
Example 2
paragraphs = [["a","bb","ccc"]]width = 4return = ["******","*a bb*","*ccc *","******"]a bb fills the whole line exactly. The last word ccc is one character short, so its one extra space goes on the right, just before the closing asterisk.
Constraints
1 <= paragraphs.length <= 104- Every paragraph contains at least one non-empty word.
1 <= word.length <= width <= 104- The total number of words and characters is at most
105. - Words contain visible non-space ASCII characters.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
Similar questions at other companies
- Robot Assembly Line ConflictsGoogle · OA
- Balance the Game BoardMicrosoft · OA
- Minimum Steps on a Circular Letter DialIBM · OA
- Word Wrap with Even JustificationSentry · OA · PHONE SCREEN
- Max Number of OperationsSalesforce · OA
- Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEW
- Choose the Next Hidden-Word GuessCharacter.AI · PHONE SCREEN
- URL Segment Compression Part 3 — Global Token BudgetStripe · ONSITE INTERVIEW
- Text JustificationUber · PHONE SCREEN
- Tennis Game ScoreAbridge · OA