Transpose File
MedPrompt
Given a text file file.txt, transpose its content.
You may assume that each row has the same number of columns, and each field is separated by the ' ' character.
Example:
If file.txt has the following content:
name age
alice 21
ryan 30Output the following:
name alice ryan
age 21 30Approaches
3 approaches with complexity analysis and trade-offs.
This approach involves reading the entire file into a 2D array or list of lists. A second 2D array of transposed dimensions is then created. The data is copied from the original matrix to the transposed matrix by swapping the row and column indices. Finally, the transposed matrix is printed to the standard output.
Algorithm
- Read all lines from the file.
- Split each line into words and store them in a 2D list,
originalData. - Determine dimensions:
rows = originalData.size()andcols = originalData.get(0).size(). - Create a new 2D array,
transposedData, of sizecols x rows. - Populate
transposedDatausing the formulatransposedData[i][j] = originalData.get(j).get(i). - Print each row of
transposedData.
Walkthrough
This approach is the most direct translation of the matrix transpose operation. It first loads the entire file content into an in-memory 2D data structure, like a List<String[]>. This represents the original matrix.
Once the data is loaded, it determines the dimensions (rows R and columns C). It then allocates a second 2D array with swapped dimensions (C x R). A nested loop iterates through the new dimensions, and for each cell (i, j) in the transposed matrix, it fetches the corresponding element from the original matrix at (j, i).
After the transposed matrix is fully populated, another loop iterates through it to print each row to the console, with elements separated by spaces.
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List; public class Solution { public void transposeFile(String filePath) throws IOException { List<String[]> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { lines.add(line.split(" ")); } } if (lines.isEmpty()) { return; } int rows = lines.size(); int cols = lines.get(0).length; String[][] transposed = new String[cols][rows]; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { transposed[i][j] = lines.get(j)[i]; } } for (int i = 0; i < cols; i++) { System.out.println(String.join(" ", transposed[i])); } }}Complexity
Time
O(R * C)
Space
O(R * C)
Trade-offs
Pros
Very straightforward and easy to reason about.
Separates the logic of data loading, transformation, and presentation.
Cons
Highest memory usage as it stores two copies of the data (in different layouts).
Not suitable for very large files that cannot fit into memory twice.
Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.