Max Value after Updating Ranges

Quick Overview

Apply a list of inclusive range increments to an all-zero array and return the largest value in the final array. Tests the difference array technique, prefix sums, linear-time range updates and correct handling of arrays whose values all end negative.

Max Value after Updating Ranges

Company: Addepar

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer `length` and an array `updates` where `updates[i] = [start_index, end_index, inc]`. You have an array `arr` of length `length` with all zeros, and you apply each update to it in turn. For the update `[start_index, end_index, inc]`, increment every element `arr[start_index]`, `arr[start_index + 1]`, ..., `arr[end_index]` by `inc`. Return the maximum value in `arr` after applying all the updates. ### Function Signature ```python def get_max_value(length: int, updates: list[list[int]]) -> int: ``` ### Rules - Both ends of an update are inclusive. - `inc` can be negative, and elements no update touches stay 0. - Return the true maximum of the final array. When every element ends negative, the answer is negative. ### Constraints - `1 <= length <= 10^5` - `0 <= len(updates) <= 10^4` - `0 <= start_index <= end_index < length` - `-1000 <= inc <= 1000` ### Examples ```text Input: length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]] Output: 5 ``` Starting from `[0, 0, 0, 0, 0]`, the array becomes `[0, 2, 2, 2, 0]`, then `[0, 2, 5, 5, 3]`, then `[-2, 0, 3, 5, 3]`. The largest value is 5, at index 3. ```text Input: length = 10, updates = [[2, 4, 6], [5, 6, 8], [1, 9, -4]] Output: 4 ``` The first two updates give `[0, 0, 6, 6, 6, 8, 8, 0, 0, 0]`, and the third subtracts 4 from indices 1 through 9, giving `[0, -4, 2, 2, 2, 4, 4, -4, -4, -4]`. The largest value is 4. ```text Input: length = 3, updates = [[0, 2, -5], [1, 1, 2]] Output: -3 ``` The final array is `[-5, -3, -5]`. Every element is negative, so the maximum is -3, not 0.

Overview: Apply a list of inclusive range increments to an all-zero array and return the largest value in the final array. Tests the difference array technique, prefix sums, linear-time range updates and correct handling of arrays whose values all end negative.

A
Addepar
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer length and an array updates where updates[i] = [start_index, end_index, inc].

You have an array arr of length length with all zeros, and you apply each update to it in turn. For the update [start_index, end_index, inc], increment every element arr[start_index], arr[start_index + 1], ..., arr[end_index] by inc.

Return the maximum value in arr after applying all the updates.

Function Signature

def get_max_value(length: int, updates: list[list[int]]) -> int:

Rules

  • Both ends of an update are inclusive.
  • inc can be negative, and elements no update touches stay 0.
  • Return the true maximum of the final array. When every element ends negative, the answer is negative.

Constraints

  • 1 <= length <= 10^5
  • 0 <= len(updates) <= 10^4
  • 0 <= start_index <= end_index < length
  • -1000 <= inc <= 1000

Examples

Input: length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]]
Output: 5

Starting from [0, 0, 0, 0, 0], the array becomes [0, 2, 2, 2, 0], then [0, 2, 5, 5, 3], then [-2, 0, 3, 5, 3]. The largest value is 5, at index 3.

Input: length = 10, updates = [[2, 4, 6], [5, 6, 8], [1, 9, -4]]
Output: 4

The first two updates give [0, 0, 6, 6, 6, 8, 8, 0, 0, 0], and the third subtracts 4 from indices 1 through 9, giving [0, -4, 2, 2, 2, 4, 4, -4, -4, -4]. The largest value is 4.

Input: length = 3, updates = [[0, 2, -5], [1, 1, 2]]
Output: -3

The final array is [-5, -3, -5]. Every element is negative, so the maximum is -3, not 0.