FastPrepWeighted Range Sum Queries
Problems/Weighted Range Sum Queries

Weighted Range Sum Queries

AMD logoAMD● MediumNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source preserves the local-weight formula and example. Input bounds and 64-bit callable interface are authored; approximate source match 95%. An exact source date is not available.

For each inclusive, one-based query [l,r], return nums[l-1] + 2*nums[l] + ... + (r-l+1)*nums[r-1]. Weights restart at 1 for every query. Preserve query order and leave the input unchanged. Use 64-bit results.

Function

weightedQueries(nums: int[], queries: int[][]) → long[]

Examples

Example 1

nums = [5,2,7,6,3]queries = [[1,3],[2,5],[4,5]]return = [30,46,12]

The three sums are 5+4+21=30, 2+14+18+12=46, and 6+6=12.

Example 2

nums = [9,-2,4]queries = [[2,2],[1,3]]return = [-2,17]

A singleton has weight 1; the whole range gives 9-4+12=17.

Constraints

  • 1 <= nums.length <= 2000.
  • -1000000 <= nums[i] <= 1000000.
  • 0 <= queries.length <= 2000.
  • Each query has exactly two entries.
  • For every query [l,r], 1 <= l <= r <= nums.length.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public long[] weightedQueries(int[] nums, int[][] queries) {
    // Write your code here.
}
Checking account…