EasyArrayMatrixUpdated Sep 2026

2639. Find the Width of Columns of a Grid

Asked at Atlassian

Difficulty

Easy

Time

O(m * n)

Space

O(n)

Pattern

Array

Companies

1

Problem

You are given a 2D grid of integers with m rows and n columns. For each column, find the width of that column.

The width of a column is the length of the longest string needed to write any number in that column. A number's length is just how many characters it takes to print: digits count one each, and a minus sign counts as one extra character. So -15 has length 3, while 7 has length 1.

Return an array of n integers, where the value at index i is the width of column i.

Measure each column separately

grid0120121-3-2-7-365-61Column 0: "1", "-7", "5" → longest is 2 charactersgrid0120121-3-2-7-365-61Column 2: "-2", "6", "1" → longest is 2 characters
For every column we look at all its numbers and keep the longest printed form.

Constraints

  • ●m == grid.length
  • ●n == grid[i].length
  • ●1 <= m, n <= 100
  • ●-10^9 <= grid[r][c] <= 10^9

Examples

Example 1
Inputgrid = [[1],[22],[333]]
Output[3]
WhyThere is only one column. Its values are 1, 22 and 333, with lengths 1, 2 and 3. The largest is 3, so the answer is [3].
Example 2
Inputgrid = [[-15,1,3],[15,7,12],[5,6,-2]]
Output[3,1,2]
WhyColumn 0 holds -15, 15 and 5, with lengths 3, 2 and 1, so its width is 3. Column 1 holds 1, 7 and 6, all length 1, so its width is 1. Column 2 holds 3, 12 and -2, with lengths 1, 2 and 2, so its width is 2.

Asked At

1 companies
CompanyDifficulty
AtlassianEasyView all Atlassian questions →

How to Think About It

1

Brute force: for each column, iterate through all rows. Convert each number to a string and track the maximum string length. Return an array of these maximums. Time: O(m*n) where m is rows and n is columns.

2

Key insight: the width depends on the string representation, not the numeric value. The number -100 has width 4 (negative sign + 3 digits), while 99 has width 2. Use len(str(num)) for each number.

3

No need for dynamic programming or complex data structures. This is a simple nested loop: outer loop over columns, inner loop over rows. Convert each number to a string and compare lengths.

4

Edge cases: single row matrix (width of each column is just the length of that number's string), single column matrix (width is the max across all rows), all same numbers (all widths equal), zero (width is 1).

Optimal Approach

Step 1: If the grid is empty, return an empty list.
Step 2: Get the number of columns from the first row: cols = len(grid[0]).
Step 3: For each column j from 0 to cols-1:

  • Initialize max_width = 0
  • For each row i, calculate width = len(str(grid[i][j]))
  • Update max_width = max(max_width, width)
  • Store max_width in the result
    Step 4: Return the result array.

Walkthrough for [[1, -3, -2], [-7, -3, 6], [5, -6, 1]]:

  • Column 0: "1"->1, "-7"->2, "5"->1. Max = 2
  • Column 1: "-3"->2, "-3"->2, "-6"->2. Max = 2
  • Column 2: "-2"->2, "6"->1, "1"->1. Max = 2
  • Result: [2, 2, 2]

Time: O(m*n) for visiting every cell. Space: O(n) for the result array.

Loop columns, then rows

grid0120121-3-2-7-365-61Step 1 — fix column 0 and read every row: lengths 1,2, 1max_width = max(max_width, len(str(grid[i][j])))column 0 → 2ans222012Step 2 — repeat for columns 1 and 2, then return thewidths
Each column is independent, so we can fill the answer one entry at a time.
ComplexityTime O(m * n)Space O(n)We touch every cell once to measure its string length, and the result array holds one number per column.

What Trips People Up in Real Interviews

1

Confusing absolute value with string length. abs(-100) = 100 but len(str(-100)) = 4. The width includes the negative sign, so you must convert to string, not take absolute value.

2

Not accounting for the negative sign. -7 is 2 characters, not 1. The problem explicitly says "including the negative sign." This is the most common mistake.

3

Using the wrong data type for the result. Return List[int] where each element is the width of the corresponding column. Not the widths as strings.

4

Transposing the grid accidentally. You need to iterate rows for each column, not columns for each row. Make sure your inner loop goes through all rows for a fixed column.

5

Forgetting that the grid might be empty. An empty grid [] should return an empty list []. A grid with rows but zero columns [[], []] should return an empty list.

Solution Code

O(m * n) time
def findColumnWidth(grid):
    if not grid or not grid[0]:
        return []
    cols = len(grid[0])
    result = []
    for j in range(cols):
        max_width = 0
        for i in range(len(grid)):
            max_width = max(max_width, len(str(grid[i][j])))
        result.append(max_width)
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Find the Width of Columns of a Grid problem?

Given a matrix of integers, find the width of each column. The width of a column is the maximum number of characters (including the negative sign) needed to represent any number in that column. This tests matrix traversal and string formatting.

How do you solve Find the Width of Columns of a Grid?

The optimal approach runs in O(m * n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Find the Width of Columns of a Grid?

Find the Width of Columns of a Grid is asked at Atlassian. It is a easy difficulty problem.

What are common mistakes on Find the Width of Columns of a Grid?
  • Confusing absolute value with string length. `abs(-100) = 100` but `len(str(-100)) = 4`. The width includes the negative sign, so you must convert to string, not take absolute value.
  • Not accounting for the negative sign. `-7` is 2 characters, not 1. The problem explicitly says "including the negative sign." This is the most common mistake.
  • Using the wrong data type for the result. Return `List[int]` where each element is the width of the corresponding column. Not the widths as strings.
  • Transposing the grid accidentally. You need to iterate rows for each column, not columns for each row. Make sure your inner loop goes through all rows for a fixed column.
  • Forgetting that the grid might be empty. An empty grid `[]` should return an empty list `[]`. A grid with rows but zero columns `[[], []]` should return an empty list.