Learn practical skills, build real-world projects, and advance your career

Subarray with Given Sum

The following question was asked during a coding interview for Amazon:

You are given an array of numbers (non-negative). Find a continuous subarray of the list which adds up to a given sum.

alt

#GT Test
Test0 = {
    'input': {
        'arr': [1, 7, 4, 2, 1, 3, 11, 5],
        'sum': 10
    },
    'output': [4, 2, 1, 3]
}
#Aakash I/O def
arr0 =  [1, 7, 4, 2, 1, 3]
target0 = 10
output0 = 2,6 # Return edge indices
#Write fn sugnature
def subarray_sum(arr, target):
  pass
#Write tests to show Test Driven Development - TDD
""" Generic array where subarr is in center
Subarr is at start
Subarr is at end
No subarr that adds to 10
You have a few zeros in list
Multiple subarrays with same sum
Empry arr
Teh subarray is a single element
"""