KatInterview

Draw a Grid Chart from Coordinate Pairs

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingarraysstringshash-map

Last 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