Format a Newspaper Page
Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Capital One OA, reported in September 2026, is carrying line state across paragraphs. "Format a Newspaper Page" looks like string padding, and it mostly is. Greedy word packing, then pad left or right to exactly width. But each paragraph starts fresh, and the off-by-one on the space between words is where people bleed test cases. You've got a quick simulation problem that punishes sloppy bookkeeping, not a hard algorithm. If you blank on the edge cases mid-assessment, StealthCoder runs invisibly as a safety net and gives you a working solution on screen.
The problem
You are given an array of paragraphs paragraphs, an array aligns, and an integer width. paragraphs[i] is the ith paragraph, represented as an ordered array of words. aligns[i] is either LEFT or RIGHT and specifies how paragraph i is aligned. width is the exact number of characters in every formatted output line. Format the paragraphs in order. Within each paragraph, preserve the word order and separate adjacent words with one space. Greedily place as many consecutive words as possible on each line without making its length exceed width. If the next word does not fit, start a new line with that word. After packing a line, fill its remaining positions with spaces. For LEFT alignment, append the spaces after the words. For RIGHT alignment, prepend the spaces before the words. Return all formatted lines in paragraph order as a string array. Each returned string must contain exactly width characters. Function formatNewspaperPage(paragraphs: String[][], aligns: String[], width: int) → String[] Examples Example 1 paragraphs = [["The","quick","brown","fox"],["jumps","over","it"]] aligns = ["LEFT","RIGHT"] width = 10 return = ["The quick ","brown fox ","jumps over"," it"] The first paragraph is left-aligned. The quick and brown fox each use 9 characters, so each receives one trailing space. The second paragraph is right-aligned. jumps over already uses all 10 characters, while it receives eight leading spaces. Constraints paragraphs.length = aligns.length. Every value in aligns is either LEFT or RIGHT. width is positive. Every word has length at most width.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a plain greedy loop per paragraph. Keep a current line length and a word list. For the next word, the needed length is current length plus 1 plus word length, or just word length if the line is empty. If that exceeds width, flush the line and start a new one with that word. The classic pitfall is adding the separator space before checking the fit, or forgetting that an empty line needs no leading space. Second pitfall: flushing the last line of each paragraph. Don't skip it, and don't let it merge into the next paragraph. On flush, join with single spaces, compute padding as width minus length, then append spaces for LEFT or prepend for RIGHT. Every output string must be exactly width characters. Runtime is linear in total characters. If the live OA rattles you, StealthCoder is the hedge that reads the problem and hands you the loop.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Format a Newspaper Page cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Capital One's OA.
Capital One reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Format a Newspaper Page FAQ
How hard is Format a Newspaper Page really?+
Easy to medium. There's no clever data structure. It's a simulation with string building. The difficulty is in the details: the separator space, the final line of each paragraph, and exact padding. If you write it carefully, it passes.
What's the trick to the greedy packing?+
Check fit before adding the word. The needed length is the current length plus one space plus the word, or only the word when the line is empty. If it exceeds width, flush the line and start a new one with that word.
What's the most common bug on this problem?+
Forgetting to flush the last line of a paragraph, or letting words from one paragraph bleed into the next. Reset your line state at the start of every paragraph and always flush at its end.
How do I handle LEFT versus RIGHT alignment?+
Join the words with single spaces, then compute padding as width minus the joined length. For LEFT, append that many spaces. For RIGHT, prepend them. A line that exactly fills the width gets zero padding either way.
How do I prepare for this in 48 hours?+
Write the solution once from scratch and test it against the sample. Then try edge cases: a single word equal to width, one-word paragraphs, and an exact-fit line. Also review similar text justification problems so the line-building loop feels familiar.