FastPrepTranspose a String Grid
Problems/Transpose a String Grid

Transpose a String Grid

Indeed logoIndeed● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The first coding question in a Karat interview was a grid transpose on text: lay a string out as rows by cols, swap rows and columns, and read the result back as one string. Example 1 uses the report's 3 by 3 example with the letters a to i.

The report does not say how the string fills the matrix or give any limits. Here, the text fills the grid row by row, and its length is exactly rows times cols. Printable ASCII characters and the size limits are practice choices.

The judged task matches the report at about 90%.

You are given a string text and two integers rows and cols. Write text into a grid with rows rows and cols columns, row by row: the first cols characters form row 0, the next cols characters form row 1, and so on.

Transpose the grid: column c of the grid, read from top to bottom, becomes row c of the new grid. The new grid has cols rows and rows columns.

Return the characters of the new grid joined row by row into one string. Every character is one cell, including spaces and punctuation.

Function

transposeString(text: String, rows: int, cols: int) → String

Examples

Example 1

text = "abcdefghi"rows = 3cols = 3return = "adgbehcfi"

Row by row, the grid is abc, def, ghi. Its first column read from top to bottom is adg, the second is beh, and the third is cfi. These become the rows of the transposed grid, so the answer is adgbehcfi.

Example 2

text = "abcdef"rows = 2cols = 3return = "adbecf"

The grid has 2 rows of 3: abc and def. The transposed grid has 3 rows of 2: ad, be and cf. Joined, they give adbecf.

Example 3

text = "to-do!"rows = 3cols = 2return = "t-ood!"

The grid has 3 rows of 2: to, -d and o!. Its columns read from top to bottom are t-o and od!. The hyphen and the exclamation mark move like any other character, so the answer is t-ood!.

Constraints

  • 1 <= text.length <= 10^4
  • 1 <= rows <= 10^4
  • 1 <= cols <= 10^4
  • rows * cols == text.length
  • text contains only printable ASCII characters (codes 32 to 126).

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public String transposeString(String text, int rows, int cols) {
        // Write your code here.
    }
}
Checking account…