
Asked in Microsoft Corporation
Word Wrap Problem Statement
You are tasked with arranging 'N' words of varying lengths such that each line contains at most 'M' characters, with each word separated by a space. The challenge is to minimize the cost, where the cost of each line is calculated as the cube of the extra space characters needed to reach 'M' characters. The total cost is the sum of all line costs.
Your Goal
Minimize the total cost to achieve this arrangement and return the minimum total cost.
Input:
The first line of input contains an integer ‘T’, representing the number of test cases. For each test case:
The first line contains two positive integers, ‘N’ and ‘M’, where 'N' is the number of words and 'M' is the maximum number of characters allowed per line.
The following 'N' lines each contain one word.
Output:
For each test case, output a single line containing an integer, which represents the minimum cost to arrange the words according to the specified conditions.
Example:
Input:
2
4 10
word1
word2
word3
word4
5 8
worda
wordb
wordc
wordd
worde
Output:
XYZ
ABC
Constraints:
1 <= T <= 100
1 <= N <= 300
1 <= |words[i]| <= 100
1 <= M <= 100
- Time Limit: 1 sec
Note:
The length of each word must be ≤ 'M'.
You cannot break a word across lines. Each word must appear fully on one line.

AnswerBot
4mo
The goal is to minimize the total cost of arranging 'N' words on each line with a maximum character limit 'M'.
Calculate the cost of each line as the cube of extra space characters needed to reach 'M'....read more
Help your peers!
Add answer anonymously...
Top Software Engineer Interview Questions Asked at Microsoft Corporation
Q. How would you approach designing a SaaS application?
Q. Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0'...read more
Q. Which programming language are you proficient in?
Interview Questions Asked to Software Engineer at Other Companies
Top Skill-Based Questions for Microsoft Corporation Software Engineer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

