EasyArraysAI interview only
Defuse The Bomb
Asked atamazongooglemicrosoft
01 · 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 each position i is replaced as follows, using the original values for every replacement:
- If
k > 0, replacecode[i]with the sum of the nextkelements after it. - If
k < 0, replacecode[i]with the sum of the previous|k|elements before it. - If
k == 0, replacecode[i]with0.
Because the array is circular, the element after code[n - 1] is code[0] and the element before code[0] is code[n - 1]. Return the decrypted array.
02 · Examples
Example 01
Input
code = [5,7,1,4], k = 3
Output
[12,10,16,13]
Each element becomes the sum of the next 3: [7+1+4, 1+4+5, 4+5+7, 5+7+1] = [12,10,16,13].
Example 02
Input
code = [2,4,9,3], k = -2
Output
[12,5,6,13]
Each element becomes the sum of the previous 2: [3+9, 2+3, 4+2, 9+4] = [12,5,6,13].
Example 03
Input
code = [1,2,3,4], k = 0
Output
[0,0,0,0]
When k is 0 every element becomes 0.
03 · Constraints
- 011 <= code.length <= 100
- 021 <= code[i] <= 100
- 03-(code.length - 1) <= k <= code.length - 1
04 · Optimal complexity
- Time
- O(n)
- 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.