KatInterview

Build Alternating Color Bands for a Trail Map API

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashingiterationchunkingapi-design

Last updated

Tests iteration logic for splitting a sequence into overlapping chunks with alternating properties

You're building a renderer for hiking trails that calls a map-snapshot service. The service requires a JSON payload describing the trail as a series of colored path segments (called bands) plus markers for the start and finish.

Your task is to construct this payload from a list of geographical points and a banding configuration.

Input

  • points: a list of dictionaries, each with "lat" and "lon" keys (e.g., [{"lat": 45.37, "lon": -121.7}, {"lat": 45.38, "lon": -121.69}, ...])
  • band_size: a positive integer specifying how many segments each band should contain
  • color_a, color_b: two color strings (e.g., "red", "#00aa00")

A segment is the line connecting two consecutive points. A trail with n points contains n − 1 segments.

Output

Return a dictionary with this structure:

{
  "markers": [
    {"type": "start", "lat": <first point lat>, "lon": <first point lon>},
    {"type": "finish", "lat": <last point lat>, "lon": <last point lon>}
  ],
  "bands": [
    {"color": <color>, "points": [<point dicts>]},
    ...
  ]
}

Band Construction Rules

  1. Partition the trail's segments into consecutive groups of band_size segments each.
  2. The last band may contain fewer than band_size segments if there aren't enough remaining.
  3. Alternate colors: the first band uses color_a, the second uses color_b, the third uses color_a, and so on.
  4. Each band's "points" list must include both endpoints of all its segments. Since consecutive bands share a boundary point, adjacent bands will have one overlapping point in their point lists.

Example

Given a trail with 9 points (thus 8 segments) and band_size = 3:

  • The 8 segments split into bands containing 3, 3, and 2 segments respectively.
  • A band with 3 segments spans 4 points; a band with 2 segments spans 3 points.
  • The bands alternate colors: color_a, color_b, color_a.
  • The last point of band 0 is the first point of band 1, and the last point of band 1 is the first point of band 2.

Constraints

  • 2 ≤ len(points) ≤ 10,000
  • 1 ≤ band_size
  • Each point dictionary contains "lat" and "lon" keys with numeric values.
  • Color strings are non-empty.

Examples

Example 1

Input: build_trail_payload([{'lat': 0, 'lon': 0}, {'lat': 1, 'lon': 1}], 1, 'red', 'blue')

Output: {'markers': [{'type': 'start', 'lat': 0, 'lon': 0}, {'type': 'finish', 'lat': 1, 'lon': 1}], 'bands': [{'color': 'red', 'points': [{'lat': 0, 'lon': 0}, {'lat': 1, 'lon': 1}]}]}

Example 2

Input: build_trail_payload([{'lat': 45.37, 'lon': -121.7}, {'lat': 45.38, 'lon': -121.69}, {'lat': 45.39, 'lon': -121.68}, {'lat': 45.4, 'lon': -121.67}], 2, 'green', 'orange')

Output: {'markers': [{'type': 'start', 'lat': 45.37, 'lon': -121.7}, {'type': 'finish', 'lat': 45.4, 'lon': -121.67}], 'bands': [{'color': 'green', 'points': [{'lat': 45.37, 'lon': -121.7}, {'lat': 45.38, 'lon': -121.69}, {'lat': 45.39, 'lon': -121.68}]}, {'color': 'orange', 'points': [{'lat': 45.39, 'lon': -121.68}, {'lat': 45.4, 'lon': -121.67}]}]}

Example 3

Input: build_trail_payload([{'lat': 0, 'lon': 0}, {'lat': 1, 'lon': 1}, {'lat': 2, 'lon': 2}, {'lat': 3, 'lon': 3}, {'lat': 4, 'lon': 4}, {'lat': 5, 'lon': 5}, {'lat': 6, 'lon': 6}, {'lat': 7, 'lon': 7}, {'lat': 8, 'lon': 8}], 3, '#00aa00', '#ff0000')

Output: {'markers': [{'type': 'start', 'lat': 0, 'lon': 0}, {'type': 'finish', 'lat': 8, 'lon': 8}], 'bands': [{'color': '#00aa00', 'points': [{'lat': 0, 'lon': 0}, {'lat': 1, 'lon': 1}, {'lat': 2, 'lon': 2}, {'lat': 3, 'lon': 3}]}, {'color': '#ff0000', 'points': [{'lat': 3, 'lon': 3}, {'lat': 4, 'lon': 4}, {'lat': 5, 'lon': 5}, {'lat': 6, 'lon': 6}]}, {'color': '#00aa00', 'points': [{'lat': 6, 'lon': 6}, {'lat': 7, 'lon': 7}, {'lat': 8, 'lon': 8}]}]}

Hints

Hint 1

A trail with n points has n−1 segments. A band covering k segments needs k+1 points (the segment endpoints).

Hint 2

Consecutive bands share their boundary point to avoid gaps. After processing a band ending at index i, the next band starts at index i, not i+1.

Hint 3

Track which band you're on (0, 1, 2, ...) to alternate colors with modulo 2.