Find the Width of Columns of a Grid
Asked at Atlassian
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.
Asked At
| Company | Difficulty | |
|---|---|---|
| Atlassian | Easy | View all Atlassian questions → |
How to Think About It
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.
Visual walkthrough for grid [[1, -3, -2], [-7, -3, 6], [5, -6, 1]]:
Column 0: |1|=1 char, |-7|=2 chars, |5|=1 char -> max=2
Column 1: |-3|=2 chars, |-3|=2 chars, |-6|=2 chars -> max=2
Column 2: |-2|=2 chars, |6|=1 char, |1|=1 char -> max=2
Result: [2, 2, 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.
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.
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.
What Trips People Up in Real Interviews
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.
Solution Code
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 resultFrequently 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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
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.