Build Alternating Color Bands for a Trail Map API
Arrays & HashingAuto-graded · 15 test casesLast 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 containcolor_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
- Partition the trail's segments into consecutive groups of
band_sizesegments each. - The last band may contain fewer than
band_sizesegments if there aren't enough remaining. - Alternate colors: the first band uses
color_a, the second usescolor_b, the third usescolor_a, and so on. - 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,0001 ≤ 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is correctly partitioning the trail into overlapping bands while alternating colors.
Understanding segments vs. points: A trail with n points defines n − 1 segments (each connecting two adjacent points). When we want a band to cover band_size segments, it must include band_size + 1 points—all the endpoints of those segments.
Overlapping bands: Consecutive bands share their boundary point. If band 0 ends at point index 5, band 1 must start at point index 5 (not 6) so the rendered path has no gap. This means after processing a band that ends at index end, the next band starts at index end (not end + 1).
Iteration logic: Track the starting index start of the current band. Compute the ending index as end = min(start + band_size, len(points) - 1) to handle the final band, which may be shorter. Extract points[start:end + 1], pick the color based on the band index parity, then advance start = end for the next iteration.
Markers: Always emit a start marker at the first point and a finish marker at the last point, regardless of banding.
Why It Works
The iteration condition start < len(points) - 1 ensures we continue as long as there are unprocessed segments. Each iteration advances start to the end of the current band, guaranteeing progress and preventing infinite loops. The min clamp handles trails where the segment count isn't evenly divisible by band_size, naturally creating a shorter final band.
Complexity
- Time: O(n), where n is the number of points. We traverse the list once, and each slice operation is proportional to the band size (constant or small relative to n).
- Space: O(n) for the output structure, which stores all points distributed across bands.
from typing import Dict, List
def build_trail_payload(points: List[Dict], band_size: int, color_a: str, color_b: str) -> Dict:
markers = [
{"type": "start", "lat": points[0]["lat"], "lon": points[0]["lon"]},
{"type": "finish", "lat": points[-1]["lat"], "lon": points[-1]["lon"]},
]
bands = []
start = 0
band_index = 0
while start < len(points) - 1:
end = min(start + band_size, len(points) - 1)
bands.append({
"color": color_a if band_index % 2 == 0 else color_b,
"points": points[start:end + 1],
})
start = end
band_index += 1
return {"markers": markers, "bands": bands}