All practice tasks

Coding (Algo)

Live Validation Ranges

Segment tree · Hard

A large form tracks invalid counts across repeated nested sections. Edits change one field while the summary requests arbitrary sections.

Apply point replacements and answer inclusive range sums of invalid counts.

Input

Call

  • Implement solve(input) and return its result. Do not print from solve.

Input object

{ counts: integer[], operations: ({type:"set", index:integer, value:integer}|{type:"sum", left:integer, right:integer})[] }

What each field means

  • counts[i] — current invalid-item count at position i.
  • operations — ordered set updates and sum queries.
  • set replaces one value. sum specifies left and right indexes, both included.

Output

Return value and rules

  • Return one integer for each sum operation, in query order.
  • Each answer is the sum of current counts from left through right, including both ends.
  • Apply every earlier update before answering a query. Updates do not add result elements.

Constraints

  • 1 <= counts.length <= 100000; at most 100000 operations
  • Counts and replacement values are in [0,1000]. 0 <= left <= right < n. All indexes are valid.
  • Return values, not JSON text. Do not mutate input.

Examples

Example 1

{
  "counts": [
    1,
    2,
    0
  ],
  "operations": [
    {
      "type": "sum",
      "left": 0,
      "right": 2
    },
    {
      "type": "set",
      "index": 1,
      "value": 0
    },
    {
      "type": "sum",
      "left": 1,
      "right": 2
    }
  ]
}

Returns

[
  3,
  0
]

Example 2

{
  "counts": [
    0
  ],
  "operations": []
}

Returns

[]

Required complexity

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

What this task practices

Live Validation Ranges 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.