Airbnb questions

Ski Path

AirbnbMedium

A ski resort is a one-way trail map. travel lists the lifts and runs as [from, cost, to]: moving from node from to node to costs cost energy. points lists [node, reward]: arriving at node earns reward points. A node that is not in points earns 0, and the starting node, named "start", earns nothing because a skier begins there rather than arriving. The same from and to pair can appear more than once with different costs. The map has no cycles, so no node repeats on a run.

A run begins at "start" and may end at any node whose id begins with END; it can pass through such a node and keep going. Its score is the total reward of the nodes it reaches (each once, the start excluded) minus the total cost of the edges it takes.

Write best_ski_score(travel, points), returning the highest score over all runs from "start" to an END node. Costs and rewards are given as integers.

Part 1: Highest-scoring run to an end

For example, with travel = [["start", 3, "A"], ["A", 4, "B"], ["B", 5, "END1"]] and points = [["A", 5], ["B", 6], ["END1", 3]] the only run is start, A, B, END1. It earns 5 + 6 + 3 = 14 and costs 3 + 4 + 5 = 12, so the result is 2.

  1. Example 1
    travel
    [["start",3,"A"],["A",4,"B"],["B",5,"END1"]]
    points
    [["A",5],["B",6],["END1",3]]
    Output
    2

    Why: The only path is start, A, B, END1. Rewards: 5 + 6 + 3 = 14. Costs: 3 + 4 + 5 = 12. The score is 14 - 12 = 2.

Constraints

  • 1 <= len(travel) <= 5000, and the graph has at most 1000 nodes; it has no cycles.
  • 0 <= cost <= 10^4 and 0 <= reward <= 10^4; each node appears at most once in points.
  • Node ids are non-empty strings of at most 20 characters, and the id comparison is case-sensitive.
  • "start" does not appear in points.
  • At least one node whose id begins with END is reachable from "start".
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Ski Path: Airbnb interview question | LoopCoach