Google First Round: Format a list of strings into a table
Summary
I had my first round interview at Google where I was asked to implement a function that formats a list of strings into a table based on a maximum line width constraint.
Full Experience
I recently had my first round interview at Google. The interviewer presented a coding challenge focused on string manipulation and formatting. The problem involved taking a list of strings and arranging them into a table, adhering to a specified maximum line width. I had to carefully consider how to calculate column widths and distribute the strings while maintaining left-alignment and maximizing the number of columns without violating the line width constraint.
Interview Questions (1)
We have a style guide (for example, lines must be at most 80 characters wide). We have a tool that automatically formats code to the style guide. One thing it can do is arrange lists of strings into a table. The strings in each column are left-aligned to be more easily readable. It leaves no holes in the table except after the last string in the last row (i.e. every cell contains a string, except possibly in the last row). Of course, the strings remain in the original order.
Example:
W = 70 (characters) S = [IsAudioBuffer, GetTimestamp, SetTimestamp, GetSampleRate, GetSampleSize, GetNumberOfChannels, GetNumberOfSamples, GetDataBuffer, GetChannel]
We can format this as follows:
IsAudioBuffer GetTimestamp SetTimestamp GetSampleRate |
GetSampleSize GetNumberOfChannels GetNumberOfSamples GetDataBuffer |
GetChannel
Given the list of strings, and a maximum number of characters per line, format the table using the maximum number of columns without violating the line width constraint. Column width should be equal