Array Interval Sums
Updated
Problem statement
You are given an integer array nums and an array of interval queries queries. Each query is a pair [left, right] of zero-based indices, with both endpoints included.
For each query, calculate the sum of nums[left] through nums[right]. Return one sum for each query, in the same order as the queries.
The array does not change between queries. Queries may overlap or repeat. If queries is empty, return an empty array. Use signed 64-bit sums; do not take a modulus.
Function
sumArrayIntervals(nums: int[], queries: int[][]) → long[]Examples
Example 1
nums = [3,-2,5,1]queries = [[0,2],[1,3],[2,2]]return = [6,4,5]The requested sums are 3 - 2 + 5 = 6, -2 + 5 + 1 = 4, and 5. Both endpoints belong to each interval.
Example 2
nums = [-5,4,-2]queries = [[0,0],[0,2],[2,2],[0,2]]return = [-5,-3,-2,-3]A one-element interval returns that element. The full-array sum is -5 + 4 - 2 = -3. Repeating the full-array query repeats its result.
Example 3
nums = [1000000000,1000000000,1000000000]queries = [[0,2],[1,2]]return = [3000000000,2000000000]The full interval totals 3000000000, which exceeds the signed 32-bit range. The second interval contains two values and totals 2000000000.
Constraints
1 <= nums.length <= 100000.0 <= queries.length <= 100000.-10^9 <= nums[i] <= 10^9.- Every query contains exactly two indices with
0 <= left <= right < nums.length. - Every sum fits in a signed 64-bit integer.
More Headlands Technologies interview questions
- Disaster RecoveryOA · Seen Nov 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
- Minimum Changes for Distinct Adjacent CharactersOA · Seen Sep 2026
- Sliding-Window Means with IEEE Special ValuesOA · Seen Sep 2026
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Exclusive Function Execution TimesOA · Seen Jan 2024
- Earliest Dependency ScheduleOA · Seen Oct 2023
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
Similar questions at other companies
- Minimum Positive Starting ValueBooking.com · OA
- Minimum AmountBelvedere Trading · OA
- Online No-Intercept Linear RegressionTwo Sigma · OATwo Sigma interview questions
- Range Sum QueriesIntuit · OAIntuit interview questions
- Shortest Distance on a Circular Bus RouteAmazon · OA
- Count Sortable SplitsGoogle · OAGoogle interview questions
- Array Challenge (QR Intern)Akuna Capital · OAAkuna Capital interview questions
- Range Negation UpdatesJP Morgan Chase · OAJP Morgan Chase interview questions
- Threshold AlertsGoldman Sachs · OAGoldman Sachs interview questions
- First Prefix Reaching a Target ScoreVisa · OAVisa interview questions