Sum of Even Numbers After Queries

MediumArrays & Hashingsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums and a list of queries, where queries[i] = [val, index]. Process the queries in order. For each one, add val to nums[index] (the change persists for later queries), and then record the sum of all **even** values currently in nums (negative even numbers count; 0 is even). Return the recorded sums as an array, one per query. Aim for O(n + q) total time.

Examples

Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
After each update nums is [2,2,3,4] (even sum 8), [2,-1,3,4] (6), [-2,-1,3,4] (2), [-2,-1,3,6] (4).
Input: nums = [1], queries = [[4,0]]
Output: [0]
nums becomes [5], which has no even values.

Constraints

  • `1 <= nums.length <= 10^4`
  • `-10^4 <= nums[i] <= 10^4`
  • `1 <= queries.length <= 10^4`
  • `-10^4 <= val <= 10^4`
  • `0 <= index < nums.length`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems