The Print Words Vertically problem gives you a sentence string and asks you to print the words vertically — character by character column by column, where each "column" contains the characters at position i from each word. Shorter words get spaces padded, but trailing spaces in each column should be removed. This coding problem tests matrix transposition with trailing space trimming. The array, string, and simulation interview pattern is the core.
Microsoft asks this to test string manipulation — specifically the ability to handle ragged column lengths and trim trailing spaces correctly. It's a simulation problem that requires careful indexing and output formatting.
Transposition with rstrip. Split sentence into words. Find max_len = maximum word length. For each column i from 0 to max_len-1: build the vertical string by taking character i from each word (or space if word is shorter). Right-strip the resulting string. Add to results.
s="TO BE OR NOT TO GO". Words=["TO","BE","OR","NOT","TO","GO"]. Max length = 3 (NOT).
Matrix transposition problems on strings require careful length handling. The "ragged columns" issue — words of different lengths — is handled by padding with spaces during column extraction. The rstrip at the end removes the padding that would be trailing. Practice similar "column-by-column extraction" problems: "transpose matrix," "rotate image," "diagonal traversal." Clean string construction with proper trimming is the key implementation skill.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Text Justification | Hard | Solve | |
| Final Value of Variable After Performing Operations | Easy | Solve | |
| Snake in Matrix | Easy | Solve | |
| Adding Spaces to a String | Medium | Solve | |
| Calculate Score After Performing Instructions | Medium | Solve |