Amazon coding test

I am having trouble understand the input system of this amazon functions

I think the example they are listing is giving you is supposed to aid in understanding what to do with the input tempChange, but you have cutoff the screenshot of the left nav which continues the example so it is hard to be sure. Basically, you are supposed to calculate aggregate temperature change for each of the days and then return the maximum of those.

NOTE: If you are clever about your algorithm, you should not have to iterate through the whole set of days for each day (you can just update the values from the previous day you just calculated).

So finishing the example they were giving you have temperature changes of 6, -2 and 5. Applying their “function” for calculating the aggregate temperature change each day, we have the aggregate temperature change on the first day is the max(6, (6 + -2 + 5)) = max(6, 9) = 9; on the second day we have max(6 + -2, -2 + 5) = max(4, 3) = 4; on the third day we have max(6 + -2 + 5, 5) = max(9, 5) = 9. So the max aggregate temperature change we should return is 9 since it is the max of the three values (9, 4, and 9).

NOTE: it looks like their example is wrong if you ask me (based on their function definition) because the value of that day is supposed to be included in both aggregate functions and it looks like they did not include it in the “next (n - i) days” calculation that they did.

Using a slightly more interesting example [-1, 2, 10, -3, 7, -4], the aggregates would be:
first = max(-1, -1 + 2 + 10 + -3 + 7 + -4) = max(-1, 11) = 11; second = max(-1 + 2, 2 + … -4) = max(1, 9) = 9; third = max(-1 + 2 + 10, 10 + … + -4) = max(11, 10) = 11; fourth = max(-1 + … + -3, -3 + 7 + -4) = max(8, 0) = 8; fifth = max(-1 + … + 7, 7 + -4) = max(15, 3) = 15; sixth = max(-1 + … + -4, -4) = max(11, -4) = 11. Therefore the max aggregate would be 15.

2 Likes

@jmrunkle thank you for reply. My problem is in the parameter of the function (List tempChange). what data structure was used?

A List is a standard collection in Java. It is probably in the collapsed imports at line 1 in your screenshot for java.util.List (possibly imported as import java.util.*). Here is the Javadoc for the List interface:

It can be iterated like this:

for (int tc : tempChange) { ... }

It can also be indexed like this:

for (int i = 0; i < tempChange.size(); i++) {
  int tc = tempChange.get(i);
  ...
}

Since you are taking a generic List and cannot assume that it is backed by an easily indexable array, it is probably best to iterate through it, but I am sure you will not be penalized by just using an index if you find that easier.

@jmrunkle thank you so much