Zigzag Conversion

Medium · Arrays & Strings · about 15 minutes

Asked at Amazon, Microsoft, Google, Meta, Bloomberg, PayPal, Goldman Sachs, JPMorgan Chase, Intuit, Visa, Capital One, Chime, SoFi, Affirm, Klarna, Morgan Stanley, American Express.

Picture writing s across numRows rows in a zigzag: go down the rows one character at a time, then climb back up diagonally, then down again, and so on. Reading the result row by row gives a new string. With "INTERVIEWPREP" and 3 rows, the layout is

I   R   W   P
N E V E P E
T   I   R

and reading it row by row gives "IRWPNEVEPETIR". Produce that row-by-row string.