KatInterview

Build a Route Map Request with Markers and Striped Segments

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashingarraysgroupingtransformation

Last 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:

KeyValue
center{"lat": 47.579, "lon": -122.31} (always)
width400 (always)
height600 (always)
zoom13 (always)
markerslist of marker objects, described below
pathslist 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:

  1. {"color": "white", "label": "Start", "coord": <first point>}
  2. {"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 s through e (inclusive, zero-based) has positions made of points s through e + 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), paths is 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.