Build a Route Map Request with Markers and Striped Segments
Arrays & HashingAuto-graded · 14 test casesLast updated
Convert longitude-first route points into a map request with start and end markers and path groups of ten segments that alternate colors and share boundary points.
A cycling route is given as a list of points in GeoJSON order, where each point is [longitude, latitude]. Write a function that builds the request payload for a static map service, as a Python dictionary.
Input
coordinates: a list of[longitude, latitude]pairs, in the order the route was ridden.
Output
Return a dictionary with exactly these keys:
| Key | Value |
|---|---|
center | {"lat": 47.579, "lon": -122.31} (always) |
width | 400 (always) |
height | 600 (always) |
zoom | 13 (always) |
markers | list of marker objects, described below |
paths | list of path objects, described below |
Coordinate objects
Every point in the output is written as {"lat": latitude, "lon": longitude}. Note that the input order is longitude first, while the output names the fields explicitly.
Markers
If coordinates is not empty, markers contains exactly two objects, in this order:
{"color": "white", "label": "Start", "coord": <first point>}{"color": "blue", "label": "Stripe", "coord": <last point>}
If coordinates is empty, markers is an empty list. If there is only one point, both markers use that same point.
Paths
A segment is the line between two consecutive points, so n points give n - 1 segments (never fewer than 0).
Split the segments, in order, into groups of 10. The last group may contain fewer than 10 segments. Each group becomes one path object:
{"color": <color>, "positions": [<coordinate objects>]}
- The first group is
"blue", the second"brown", the third"blue", and so on, alternating. - A group covering segments
sthroughe(inclusive, zero-based) haspositionsmade of pointssthroughe + 1, in route order. So a full group has 11 positions, and consecutive paths share their boundary point so the drawn line is continuous. - If there are no segments (0 or 1 points),
pathsis an empty list.
Constraints
0 <= len(coordinates) <= 10^4- Each point has exactly two numbers:
[longitude, latitude]. - Numeric values must be copied through unchanged (no rounding).
- The function does not perform any network calls; it only builds the dictionary.
Examples
Example 1
Input: build_route_map_request([[-122.2851, 47.55126], [-122.286, 47.552]])
Output: {'center': {'lat': 47.579, 'lon': -122.31}, 'width': 400, 'height': 600, 'zoom': 13, 'markers': [{'color': 'white', 'label': 'Start', 'coord': {'lat': 47.55126, 'lon': -122.2851}}, {'color': 'blue', 'label': 'Stripe', 'coord': {'lat': 47.552, 'lon': -122.286}}], 'paths': [{'color': 'blue', 'positions': [{'lat': 47.55126, 'lon': -122.2851}, {'lat': 47.552, 'lon': -122.286}]}]}
Hints
Hint 1
Write a small helper that turns `[lon, lat]` into `{"lat": lat, "lon": lon}` and use it everywhere.
Hint 2
Count segments as `max(0, n - 1)` and step through them with a stride of 10; the group number decides blue or brown.
Hint 3
A group that starts at segment index `start` and ends before `end` needs points `start` through `end` inclusive, so slice with `end + 1` to share the boundary point.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Define a helper that converts a
[longitude, latitude]pair into{"lat": latitude, "lon": longitude}. - If the route has any points, build the two markers from the first and last points. Otherwise leave the markers list empty.
- Compute the number of segments as
max(0, n - 1). Walk through the segments with a stride of 10. For each group starting at segmentstart, the group ends atend = min(start + 10, segment_count). The path's positions are the points from indexstartthrough indexendinclusive, which isroute_points[start:end + 1]. - Color each path by group parity: even-numbered groups are blue, odd-numbered groups are brown.
- Return the dictionary with the fixed map parameters plus the markers and paths.
Why it is correct
- A group of
ksegments touchesk + 1points, so slicing toend + 1gives exactly the points needed. The last point of one group is the first point of the next, so the paths join with no gap. - Taking
min(start + 10, segment_count)makes the final, shorter group include the remainder without running past the end of the list. - With 0 or 1 points,
segment_countis 0, the range is empty, and no paths are produced. A single point still gets both markers because the first and last points coincide. - Values are only moved into new dictionaries, never modified, so numbers are preserved exactly.
Complexity
- Time: O(n), since each point is converted a small constant number of times (at most twice when it is a group boundary).
- Space: O(n) for the output.
Reference solution
from typing import Dict, List
def build_route_map_request(route_points: List[List[float]]) -> Dict[str, object]:
def to_coord(point):
longitude, latitude = point
return {"lat": latitude, "lon": longitude}
markers = []
if route_points:
markers = [
{"color": "white", "label": "Start", "coord": to_coord(route_points[0])},
{"color": "blue", "label": "Stripe", "coord": to_coord(route_points[-1])},
]
paths = []
segment_count = max(0, len(route_points) - 1)
for group_index, start in enumerate(range(0, segment_count, 10)):
end = min(start + 10, segment_count)
paths.append({
"color": "blue" if group_index % 2 == 0 else "brown",
"positions": [to_coord(point) for point in route_points[start:end + 1]],
})
return {
"center": {"lat": 47.579, "lon": -122.31},
"width": 400,
"height": 600,
"zoom": 13,
"markers": markers,
"paths": paths,
}