Question bank › Dynamic Programming
Dsa Dynamic Programming Medium

Align The Text

You are tasked with formatting a series of words into a specified line width. Given a list of words and a maximum line width, your goal is to determine the minimum cost of aligning the text. The cost is calculated as the cube of the number of extra spaces in a line if the line exceeds the maximum width. If a line can fit within the maximum width, its cost is 0. Return the minimum total cost to format all lines. Input format: - The first line contains an integer n (number of words). - The second line contains n space-separated words. - The third line contains an integer w (maximum width). Output format: - A single integer representing the minimum cost of formatting the text. Example: Input: 4 This is an example 10 Output: 1

Key concepts

dynamic_programmingstring

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.