All practice tasks

Coding (Algo)

Discounted Service Route

Shortest path · Hard

A service request travels through directed connections with non-negative costs. One connection on the route may use a promotional capacity allocation that halves its cost. The best route with this option need not be the ordinary cheapest route.

Find the minimum source-to-target cost when at most one traversed connection may cost floor(cost / 2).

Input

Call

  • Implement solve(input); return the result without mutating input.

Graph

  • n is the number of services, with IDs 0..n-1.
  • Each [from, to, cost] is a directed connection. Costs are non-negative integers.
  • source and target are service IDs. Parallel connections, self-edges and zero costs are allowed.

Discount

  • At most one traversed connection may use floor(cost / 2). All other traversals pay their full cost.

Output

Return value

  • Return the smallest total cost over all valid directed routes and choices of discounted connection.
  • Return null when target is unreachable. Return 0 if source equals target.
  • Return the cost only; do not return the route or the selected connection.

Constraints

  • 1 <= n <= 50000; 0 <= edges.length <= 100000.
  • 0 <= cost <= 1000000; every endpoint is valid. Totals fit a JavaScript safe integer.

Examples

Example 1

{
  "edges": [
    [
      0,
      2,
      9
    ],
    [
      0,
      1,
      4
    ],
    [
      1,
      2,
      4
    ]
  ],
  "n": 3,
  "source": 0,
  "target": 2
}

Returns

4

The direct route becomes best after discount; discounting the ordinary cheapest route would cost 6.

Example 2

{
  "edges": [],
  "n": 1,
  "source": 0,
  "target": 0
}

Returns

0

Required complexity

O((n + m) log(n + m)) time and O(n + m) space

What this task practices

Discounted Service Route is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.