Cells in a Range on an Excel Sheet - Problem
Excel Sheet Range Generator

Imagine you're working with an Excel spreadsheet and need to programmatically generate all cell references within a given range. In Excel, cells are represented using a combination of:

  • Column letters: A, B, C, ... Z, AA, AB, ... (like a base-26 numbering system)
  • Row numbers: 1, 2, 3, ... (simple integers)

For example, cell B3 means column B (2nd column) and row 3.

The Challenge: Given a range string like "A1:F1" (from A1 to F1) or "K1:L2" (from K1 to L2), you need to return all cell references in that rectangular range.

Important: The output should be sorted column-first (like reading a book: left to right, then top to bottom). So for range "A1:B2", the order would be: ["A1", "A2", "B1", "B2"].

Input & Output

example_1.py โ€” Basic Range
$ Input: s = "K1:L2"
โ€บ Output: ["K1","K2","L1","L2"]
๐Ÿ’ก Note: The range spans 2 columns (K, L) and 2 rows (1, 2). We generate column-first: all K cells first (K1, K2), then all L cells (L1, L2).
example_2.py โ€” Single Row Range
$ Input: s = "A1:F1"
โ€บ Output: ["A1","B1","C1","D1","E1","F1"]
๐Ÿ’ก Note: This is a horizontal range spanning 6 columns but only 1 row. Each column contributes exactly one cell.
example_3.py โ€” Single Cell
$ Input: s = "A2:A2"
โ€บ Output: ["A2"]
๐Ÿ’ก Note: Edge case where start and end are the same cell. The range contains only one cell: A2.

Constraints

  • The length of s is in range [1, 7]
  • s consists of uppercase English letters, digits and ':'
  • s is a valid range string in the format "<col1><row1>:<col2><row2>"
  • 1 <= col1 <= col2 <= 26 (only single letters A-Z)
  • 1 <= row1 <= row2 <= 9
  • The range is guaranteed to be valid (col1 โ‰ค col2 and row1 โ‰ค row2)

Visualization

Tap to expand
Excel Range Generation: Column-First OrderABC12A1Order: 1A2Order: 2B1Order: 3B2Order: 4C1Order: 5C2Order: 6Algorithm Steps1. Parse range: 'A1:C2'2. Column A: generate A1, A23. Column B: generate B1, B24. Column C: generate C1, C25. Result: [A1, A2, B1, B2, C1, C2]Time: O(rows ร— cols) | Space: O(rows ร— cols)Column-first traversal
Understanding the Visualization
1
Parse the Range
Split 'A1:C2' into start coordinates (A,1) and end coordinates (C,2)
2
Column A Processing
Generate all cells in column A: A1, A2 (rows 1 to 2)
3
Column B Processing
Generate all cells in column B: B1, B2 (rows 1 to 2)
4
Column C Processing
Generate all cells in column C: C1, C2 (rows 1 to 2)
5
Combine Results
Final ordered list: [A1, A2, B1, B2, C1, C2]
Key Takeaway
๐ŸŽฏ Key Insight: Excel ranges follow column-first ordering, not row-first. This is like reading a book where you finish each page (column) completely before moving to the next page, rather than reading line by line across multiple pages.
Asked in
Google 15 Microsoft 25 Amazon 12 Meta 8
38.7K Views
Medium Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen