← Questions

Apply range overwrite queries

0
GoogleCodingLast asked Feb 2026

A coding interview question asked in Google's onsite coding round, last asked Feb 2026. It tests segment trees, arrays and range updates.

You get an integer array nums and a list of queries (left, right, value). Each query sets every element from index left to index right (inclusive) to value. Queries apply in order, so later ones overwrite earlier ones where they overlap. Return the array after all queries, as efficiently as possible.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • apply_overwrites(nums: list[int], queries: list[tuple[int, int, int]]) -> list[int]

Example

apply_overwrites([1, 2, 3, 4, 5], [(1, 3, 9), (2, 4, 7)])
  ->  [1, 9, 7, 7, 7]
# after (1,3,9): [1, 9, 9, 9, 5]; after (2,4,7): [1, 9, 7, 7, 7]

apply_overwrites([0, 0, 0, 0, 0, 0], [(0, 5, 1), (2, 3, 2), (1, 2, 3)])
  ->  [1, 3, 3, 2, 1, 1]

apply_overwrites([5, 6, 7], [])  ->  [5, 6, 7]

Constraints

  • 0 <= n, q <= 2 * 10^5; values in [-10^9, 10^9]; 0 <= left <= right < n.
  • The naive O(n * q) loop is too slow.

Clarifying questions

  • Are queries applied strictly in order? Are ranges inclusive?
  • Are all queries known up front (offline), or do they arrive with reads in between (online)?
  • Can left > right occur?

What a strong answer covers

  • The offline trick: process queries from last to first. The first time an index is written in reverse order, that is its final value, so each index needs to be written only once.
  • Skip already-final indices with a "next unfilled index" union-find (path compression): O((n + q) * α(n)) overall.
  • Alternatives: a sweep over positions with a max-heap of active queries keyed by query index, O((n + q) log q); a segment tree with lazy assignment, needed when reads interleave with updates.
  • Tests: no queries, one query covering everything, nested and identical ranges, single-element ranges.

Follow-ups

  • Point reads get(i) arrive between updates (online): lazy segment tree, or an interval map of runs.
  • Range sum queries between updates.
  • Report how many distinct values the final array has.

Where it's asked

  • Google · Onsite coding · Last asked Feb 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

segment trees · arrays · range updates

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding