Draw a Grid Chart from Coordinate Pairs
Arrays & HashingAuto-graded · 19 test casesLast updated
Tests array processing, coordinate mapping, and ASCII visualization by building a bordered grid from unsorted integer coordinates.
You are given a collection of integer coordinate pairs (x, y). Your task is to construct an ASCII grid visualization where each coordinate is marked with an asterisk.
The grid must satisfy these requirements:
- The x-axis spans every integer from the smallest x-coordinate to the largest x-coordinate in ascending order (left to right)
- The y-axis spans every integer from the largest y-coordinate down to the smallest y-coordinate (top to bottom)
- Each coordinate pair is represented by a
*character positioned in its corresponding grid cell - The grid is enclosed by a rectangular border using
+characters - Horizontal borders (top and bottom) consist of
+symbols connected by five dashes (-----), with one segment per x-coordinate column - Vertical borders and cell separators use
+symbols - Each cell is exactly 5 characters wide, with the
*centered at position 2 (zero-indexed) - Empty cells contain only spaces
- When multiple pairs share the same coordinates, display only one
* - Return the visualization as a list of strings, one string per row
For empty input, return an empty list.
Constraints:
- 0 ≤ number of pairs ≤ 10,000
- -10,000 ≤ x, y ≤ 10,000
- Each pair contains exactly two integers
- The input order may be arbitrary
Examples
Example 1
Input: draw_coordinate_grid([[1, 2], [6, 3], [4, 5], [2, 2]])
Output: ['+-----+-----+-----+-----+-----+-----+', '+ + + + * + + +', '+ + + + + + +', '+ + + + + + * +', '+ * + * + + + + +', '+-----+-----+-----+-----+-----+-----+']
Example 2
Input: draw_coordinate_grid([[0, 0]])
Output: ['+-----+', '+ * +', '+-----+']
Example 3
Input: draw_coordinate_grid([[-2, -1], [1, 2], [0, 0], [-2, 2]])
Output: ['+-----+-----+-----+-----+', '+ * + + + * +', '+ + + + +', '+ + + * + +', '+ * + + + +', '+-----+-----+-----+-----+']
Hints
Hint 1
Consider using a set to track which coordinates contain points for fast lookup and automatic deduplication
Hint 2
Build the grid from top to bottom, iterating y-values from maximum to minimum to match the visual orientation
Hint 3
Precompute the border string once since it appears at both the top and bottom of the output
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core strategy is to treat this as a grid-rendering problem where we map coordinates to cells in a 2D ASCII canvas.
First, determine the bounding box by finding the minimum and maximum values for both x and y coordinates. This defines the dimensions of our grid: every integer x-value from minimum to maximum becomes a column, and every integer y-value from maximum to minimum becomes a row.
Next, store all coordinate pairs in a hash set. This enables constant-time lookups to check whether a specific grid cell should contain an asterisk, and automatically handles duplicate coordinates by treating them as a single point.
Then construct the output row by row from top to bottom. Since larger y-values should appear at the top of the visualization, iterate y-values in descending order. For each row, iterate through all x-values in ascending order (left to right). At each cell position, check if the coordinate pair exists in the set: if present, place a centered asterisk (' * '); otherwise, use five spaces (' '). Join these cells with + separators and wrap the row with + characters at both ends.
Finally, construct the border string using repeated ----- segments separated by + symbols, and place this border at both the beginning and end of the output.
Why It Works
This approach guarantees completeness: every integer within the x-range gets its own column, and every integer within the y-range gets its own row, regardless of whether any actual coordinate uses that value. Blank rows and columns naturally appear where no points exist.
The hash set ensures that duplicate coordinates produce exactly one asterisk per cell, and provides O(1) lookup time for each cell we render.
The formatting maintains consistency: both the border and interior rows have identical width since they use the same column structure.
Complexity
Let n be the number of coordinate pairs, W = (max_x - min_x + 1) be the width, and H = (max_y - min_y + 1) be the height.
Time Complexity: O(n + W·H)
- Computing extremes and building the set takes O(n)
- Rendering requires visiting each of the W·H cells once with O(1) set lookup per cell
- String construction adds O(W) per row, contributing O(W·H) total
Space Complexity: O(n + W·H)
- The coordinate set stores at most n entries
- The output grid contains H + 2 strings, each of length approximately 6W, resulting in Θ(W·H) space
Note that the complexity depends on the coordinate value ranges, not just the number of input pairs. Two coordinates like (0, 0) and (1000, 1000) would generate a very large grid, but this is inherent to the problem specification.
from typing import List
def draw_coordinate_grid(coordinates: List[List[int]]) -> List[str]:
if not coordinates:
return []
x_coords = [pair[0] for pair in coordinates]
y_coords = [pair[1] for pair in coordinates]
min_x = min(x_coords)
max_x = max(x_coords)
min_y = min(y_coords)
max_y = max(y_coords)
x_range = list(range(min_x, max_x + 1))
coord_set = {(x, y) for x, y in coordinates}
border = '+' + '+'.join(['-----'] * len(x_range)) + '+'
result = [border]
for y in range(max_y, min_y - 1, -1):
cells = []
for x in x_range:
cells.append(' * ' if (x, y) in coord_set else ' ')
result.append('+' + '+'.join(cells) + '+')
result.append(border)
return result