38The best way down a trail park
38 The best way down a trail park
An Airbnb software engineer candidate reported a coding round about skiing from the top of a mountain to one of several finish checkpoints at the bottom, and finding the best-scoring way down (1 report). Minimum Cost to Reach Destination in Time, on an Airbnb LeetCode tag list (1 source), has the shape of level 3: one more number in the state, there the minutes used. This is practice in that shape, not the reported question or LeetCode's version.
Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/38_trail_scores -k level1. Earlier levels must keep passing.
The park, at every level. A downhill bike park. stretches is a list of (upper, lower, score) triples. Each
one is a one-way stretch of trail from junction upper down to junction lower, worth score points: a whole
number, which is negative for a stretch that's more work than fun. Junction names are strings. Every stretch runs
downhill, so no route ever comes back to a junction it has already passed. Two stretches can join the same two
junctions. start is the trailhead, and exits is a list of the junctions where a ride may end. Either can be a
junction that's in no stretch.
A route starts at start, rides stretches one after another, and ends at an exit. It may pass through an exit
and keep riding. A route's score is the sum of its stretches' scores. If start is itself an exit, the route that
rides nothing is allowed, and it scores 0.
Parks can be deep. One test has 50,000 junctions in a row.
stretches = [
("top", "pines", 4),
("top", "ridge", 7),
("top", "creek", 2),
("pines", "bowl", 6),
("ridge", "bowl", 1),
("ridge", "mill", -3),
("creek", "mill", 5),
("bowl", "gate", 3),
("bowl", "meadow", 2),
("mill", "meadow", 4),
("gate", "lot", 2),
("meadow", "lot", 1),
("meadow", "lot", -2), # a second, duller stretch between the same two junctions
]
exits = ["gate", "lot"]
Level 1. Write best_score(stretches, start, exits): the highest score of any route, or None when no exit can
be reached from start. In the park above it's 15, from top, pines, bowl, gate, lot. That route rides
through the exit at gate and on to lot, because the stretch below gate is worth 2 more.
Level 2. Write best_route(stretches, start, exits): the list of junctions on a route with the highest score,
start first and the exit last, or None when no exit can be reached. Above, it's
["top", "pines", "bowl", "gate", "lot"].
When several routes share the highest score, return the one whose list comes first in Python's list order, the
order < uses on lists: the first junction where two lists differ decides, by its name, and a list that stops
where the other keeps going comes first. If ("ridge", "bowl", 1) scored 3 instead, ["top", "ridge", "bowl", "gate", "lot"] would also score 15, and the answer would still be the route through pines, because "pines" < "ridge". If ("gate", "lot", 2) scored 0 instead, the route could stop at gate or ride on to lot for the same
13, and the answer would be ["top", "pines", "bowl", "gate"].
Level 3. Some stretches are for experts only. Write best_score_limited(stretches, start, exits, expert, limit).
expert is a set of positions in stretches, 0 for the first, that mark the expert stretches, and a route may ride
at most limit of them. limit is a whole number from 0 up to len(expert). Return the highest score of a route
within the limit, or None when no route is. With expert = {0, 7}, the stretches top to pines and bowl to
gate, the answer is 15 with a limit of 2, 13 with a limit of 1, and 12 with a limit of 0.
Things to get right: a junction no route reaches, scores below zero, an exit partway down, two stretches between the same junctions, a park listed bottom first, a park deeper than Python's recursion limit, the tie rule, and a limit that one number per junction can't keep track of.
Be ready to talk about what comes next: how many routes share the best score, what changes if stretches could climb back up so the park has a loop, a budget of minutes in place of a count of expert stretches, and the memory the level 3 table takes when the limit is large.