Amazon Interview Experience -Coding Assessment

The following question was asked in the Amazon Online Assessment 2022 for Software Development Engineering (SDE) profile. 

Alexa is Amazon’s virtual AI assistant. It makes it easy to set up your Alexa-enabled devices, listen to music, get weather updates, and much more. The team is working on a new feature that evaluates the aggregate temperature change for a period based on the changes in temperature of previous and upcoming days. 

Taking the change in temperature data of n days, the aggregate temperature change evaluated on the i-th day is the maximum of the sum of the changes in temperatures until the i-th day, and the sum of the change in temperatures in the next (n-i) days, with the i-th day temperature change included in both.

Given the temperature data of n days, find the maximum aggregate temperature change evaluated among all the days.

Example:

tempChange = [6, -2, 5]

  • The aggregate temperature change on each day is evaluated as: 
  • For day 1, there is no preceding days’ information, but the day itself is included in the calculation. Temperature changes = [6] for the before period. 
  • For succeeding days, there are values [6, -2, 5] and 6 + (-2) + 5 = 9. Again, the change for day 1 is included. The maximum of 6 and 9 is 9.
  • For day 2, consider [6, -2] -> 6 + (-2) = 4, and [-2, 5] -> (-2) + 5 = 3. The maximum of 3 and 4 is 4. 
  • For day 3, consider [6, -2, 5] -> 6 + (-2) + 5 = 9, and [5]. The maximum of 9 and 5 is 9. 
  • The maximum aggregate temperature change is max(9, 4, 9) = 9.

Function Description

Complete the function getMaxAggregateTemperatureChange, getMaxAggregateTemperatureChange has the following parameter: 

        int tempChange[n]: the temperature change data of n days

Returns: long: the maximum aggregate temperature change

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= tempChange[i] <= 10^9 where, 1 <= i <= n

Sample Case #0

  • 3 -> tempChange[] size n
  • [-1, 2, 3] -> tempChange

Sample Output #0: 5

Sample Case #1

  • 5 -> tempChange[] size n
  • [-6, -7, -8, 1, -6] -> tempChange

Sample Output #0: -5

Solution

Python
def getMaxAggregateTemperatureChange(tempChange):
    n, sum1, sum2, aggrTempChng = len(tempChange), 0, sum(tempChange), tempChange[0]
    for i in range(n):
        if i != 0:
            sum2 -= tempChange[i-1]
        sum1 += tempChange[i]
        aggrTempChng = max(max(sum1, sum2), aggrTempChng)
    return aggrTempChng

Contact Us