Problem Definition
Suppose that you have a list s of m lists, with each s[i] containing
n nonempty strings. We want to print these strings as a table in a
terminal. The table needs to have n columns, the kth one having width
w[k]. Each pair of adjacent columns must be separated by a pipe
character ('|').
Visualization:
s[0][0] |s[0][1] | ... |s[0][n-1]
s[1][0] |s[1][1] | ... |s[1][n-1]
...
s[m-1][0]|s[m-1][1]| ... |s[m-1][n-1]
If a string s[i][k] is longer than than the width w[k] of its column,
it will be wrapped. This means that the row of the table will span at
least ceil(len(s[i][k]) / w[k]) lines of text. For example, if you
need to print the string 'abcdefghij' in a column of width 4, it
should look like
...|abcd|...
...|efgh|...
...|ij |...
and so the row containing that string must span at least 3 lines of
text.
Given s and w, print the table. (No need to worry about space/time complexity.)
Examples:
Input:
s = [
["abc", "defgh"],
["ij", "klmnopq"]
]
w = [2, 5]
Output:
ab|defgh
c |
ij|klmno
` |pq
Input:
s = [
["123456789012", "1234567", "1234567890"],
["123", "123456789012345", "12345"],
["12345", "12345678901", "12"],
]
w = [3, 7, 3]
Output:
123|1234567|123
456| |456
789| |789
012| |0
123|1234567|123
|8901234|45
|5 |
123|1234567|12
45 |8901 |
My Solution
class Solution {
public static void main(String[] args) {
List<List<String>> input = new ArrayList<>(); // this is s
List<String> s1 = List.of("123456789012", "1234567", "1234567890");
List<String> s2 = List.of("123", "123456789012345", "12345");
List<String> s3 = List.of("12345", "12345678901", "12");
input.add(s1);
input.add(s2);
input.add(s3);
int [] w = new int[]{3,7,3};
for (List<String> currentList : input) {
handlePrint(currentList, w);
}
return;
}
// currList.size() == w
private static void handlePrint(List<String> currList, int [] w) {
double maxRows = Integer.MIN_VALUE;
int index = 0;
for (String str : currList) {
maxRows = Math.max(maxRows, Math.ceil((double)str.length()/(double)w[index++]));
}
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < w.length; j++) {
String currString = currList.get(j);
int currRowSize = w[j];
for (int k = i * currRowSize; k < (i * currRowSize) + (currRowSize); k++) {
if (k < currString.length()) System.out.print(currString.charAt(k));
else System.out.print(" ");
}
if (j != w.length -1) System.out.print("|");
}
System.out.println();
}
}
}Follow up
Suppose that the total width of the columns can be at most W, i.e.
sum(w) <= W. Given s and W, find the distribution of column widths w
that minimizes the height of the tallest row. (If there are multiple
such w, any of them is fine.)
Example:
Input:
s = [
["123456789012", "1234567", "1234567890"],
["123", "123456789012345", "12345”],
["12345", "12345678901", "12"],
]
W = 15
W = 5
Sample outputs:
w = [5, 5, 5]: good (row heights are 3, 3, 3; tallest row has height 3)
w = [5, 6, 4]: good (row heights are 3, 3, 2; tallest row has height 3)
w = [4, 5, 4]: good (row heights are 3, 3, 3; tallest row has height 3)
w = [6, 6, 3]: bad (row heights are 4, 3, 2; tallest row has height 4)
Interviewer said that it would be fine just to discuss how to solve it as didn't have much time left. I suggested Finding the largest string in S then trying to minimize the number of rows for that and therefore it should minimize the number of rows for all the other strings. However, I wasn't really sure of how to achieve that. I did a naive solution of starting at a max for w for the largest string and a min for all other strings and then bringing those values together and trying to minimize the number of rows. So for the example above the largest string would be 123456789012345 .
So w would start as [1,13,1] -> [2,11,2] -> [3, 9, 3] -> [5, 7, 5] -> [7,5,7]. However, I don't think this works for all solutions and additionally for a larger w.length then the numbers might not partition so cleanly.
So I wasn't really able to come up with a solution for this.


