Code Room
CodingHardcod-g296
Subject Segment treeLevel Senior–Staff~40 minCommon in Databases & SQL · Algorithms & data structures interviewsIndustries Software development, Technology

Question

Implement a segment tree with lazy propagation over an array of length n initialized to all zeros. Process a list of operations: ["add", l, r, v] adds v to every element in the inclusive range [l, r], and ["sum", l, r] returns the sum of that inclusive range. Indices are 0-based and 0 <= l <= r < n. Return the list of answers to the "sum" operations, in order.

Implement
seg_lazy_range_add_sum(n: int, ops: list[list]) → list[int]
Examples
in[5,[["add",0,4,2],["sum",0,4],["add",1,3,3],["sum",1,3],["sum",0,4]]]out[10,15,19]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.