Magicsheet logo

Find the Width of Columns of a Grid

Easy
58.7%
Updated 6/1/2025

Asked by 2 Companies

Find the Width of Columns of a Grid

1. What is this problem about?

The Find the Width of Columns of a Grid interview question is a matrix simulation task. You are given a grid of integers. The "width" of a column is defined as the length of the longest string representation among all integers in that column. You need to return an array containing the width of each of the columns.

2. Why is this asked in interviews?

Companies like Samsung and Atlassian ask the Find the Width of Columns coding problem to assess basic string formatting and 2D array traversal skills. It evaluations your ability to handle negative numbers (where the '-' sign counts towards the length) and correctly identify the maximum value in a column-wise scan. It’s an essential Matrix interview pattern for formatting data reports.

3. Algorithmic pattern used

This problem follows the Column-wise Matrix Traversal pattern.

  1. Outer Loop: Iterate through each column index jj.
  2. Inner Loop: Iterate through each row index ii.
  3. String Conversion: For each element grid[i][j], convert the integer to a string.
  4. Max Length: Track the maximum string length encountered in the current column jj.
  5. Result: Append the maximum length to the final output array.

4. Example explanation

Grid:

[[-10, 3], 
 [5, 100]]
  • Column 0: Elements are -10 and 5.
    • "-10" length is 3.
    • "5" length is 1.
    • Max is 3.
  • Column 1: Elements are 3 and 100.
    • "3" length is 1.
    • "100" length is 3.
    • Max is 3. Result: [3, 3].

5. Common mistakes candidates make

  • Row-wise Scan: Processing rows first and forgetting to transpose the logic to columns.
  • Negative Signs: Failing to account for the '-' character in the length of negative integers.
  • Memory Overhead: Creating too many intermediate string objects in a large grid. Using math.log10 for positive numbers could be a potential optimization discussed with the interviewer.

6. Interview preparation tip

Be comfortable with the difference between row-major and column-major traversal. Most programming languages store arrays in row-major order, so iterating by columns can be slightly less cache-efficient, but it is often required for data processing tasks.

Similar Questions