All practice tasks

Coding (Algo)

Choose the Most Valuable Bookings

Dynamic programming · Medium

A single resource receives booking requests with a start time, end time and value. Requests may overlap, and selecting the earliest finish or highest individual value does not necessarily maximize the total. The service may decline every request if none improves the result.

Return the maximum sum of values for a set of pairwise non-overlapping half-open bookings without forcing selection of a negative-value request.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ intervals: [integer, integer, integer][] }

Fields

  • Each [start,end,value] has start < end and occupies [start,end). A booking ending at t is compatible with one starting at t.
  • Requests may repeat, but overlapping duplicates cannot both be accepted. Values may be negative, zero or positive.

Output

Return value

  • Return one integer total value. Empty input or exclusively negative requests returns zero.
  • The output is not the number of bookings and does not include the chosen schedule.

Constraints

  • 0 <= intervals.length <= 100000; endpoints and values are integers from -1000000 to 1000000.
  • All intervals have positive duration. Do not mutate input. Total values fit safe JSON integers.

Examples

Example 1

{
  "intervals": [
    [
      0,
      4,
      7
    ],
    [
      0,
      2,
      4
    ],
    [
      2,
      4,
      4
    ]
  ]
}

Returns

8

Example 2

{
  "intervals": [
    [
      0,
      1,
      -2
    ],
    [
      1,
      2,
      0
    ]
  ]
}

Returns

0

Required complexity

O(n log(n + 1)) time and O(n) auxiliary space

What this task practices

Choose the Most Valuable Bookings 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 medium 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.