Skip to content
MediumArraysAI interview only

Sum of Even Numbers After Queries

Asked atgoogleamazonbloomberg

01 · Problem

You are given an integer array nums and an array queries where queries[i] = [val_i, index_i].

Process the queries in order. For query i, first add val_i to nums[index_i] (the change persists for later queries), then record the sum of all even values currently in nums. Negative even numbers and 0 count as even.

Return an array answer where answer[i] is the sum recorded after the i-th query.

02 · Examples

Example 01
Input
nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output
[8,6,2,4]

After [1,0] nums = [2,2,3,4], even sum 8. After [-3,1] nums = [2,-1,3,4], even sum 6. After [-4,0] nums = [-2,-1,3,4], even sum 2. After [2,3] nums = [-2,-1,3,6], even sum 4.

Example 02
Input
nums = [1], queries = [[4,0]]
Output
[0]

nums becomes [5], which has no even values, so the sum is 0.

Example 03
Input
nums = [0,-2,5], queries = [[3,2],[-1,0]]
Output
[6,6]

After [3,2] nums = [0,-2,8], even sum 6. After [-1,0] nums = [-1,-2,8], even sum 6.

03 · Constraints

  • 011 <= nums.length <= 104
  • 02-104 <= nums[i] <= 104
  • 031 <= queries.length <= 104
  • 04-104 <= val_i <= 104
  • 050 <= index_i < nums.length

04 · Optimal complexity

Time
O(n + q)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.