Defuse the Bomb

EasySliding Windowsliding window

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 a circular integer array code of length n and an integer k. Build a new array of the same length where every position is replaced at the same time: - If k > 0, position i becomes the sum of the k elements that come after it (wrapping around). - If k < 0, position i becomes the sum of the |k| elements that come before it (wrapping around). - If k == 0, every position becomes 0. Return the new array. All sums use the original values.

Examples

Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]
For index 0: 7+1+4 = 12; for index 3: 5+7+1 = 13 (wrapping).
Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
With k = 0 everything becomes 0.
Input: code = [2,4,9,3], k = -2
Output: [12,5,6,13]
For index 0 the two previous elements are 3 and 9.

Constraints

  • n == code.length
  • 1 <= n <= 100
  • 1 <= code[i] <= 100
  • -(n - 1) <= k <= n - 1

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 sliding window problems