Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Array Sum Problem
00:00
5 left

Array Sum Problem

EasyPython

Problem

Grazitti Interactive's analytics tooling receives an integer sequence and many requests for the total value within a contiguous index range. Given an array nums and a list of inclusive range queries, return the sum for every query efficiently.

Formal Specification

Implement range_sum_queries(nums, queries). The parameter nums is a list of integers. Each query is a two-element list [left, right], where 0 <= left <= right < len(nums). Return a list of integers, where the result at position i is the sum of nums[left:right + 1] for queries[i].

Use preprocessing so that repeated queries do not require rescanning the array. Do not modify nums or queries.

Constraints

  • 1 <= len(nums) <= 100000
  • 1 <= len(queries) <= 100000
  • -10^9 <= nums[i] <= 10^9
  • 0 <= left <= right < len(nums) for every query
  • Use inclusive range boundaries

Function Signature

def range_sum_queries(nums, queries):
Interviewer

Your question is Array Sum Problem. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.