Max Range Sum — JP Morgan Chase Interview Question

Max Range Sum

Difficulty : Easy
Time Limit : 30 min

Description
Bob is developing a new strategy to get rich in the stock market. He wishes to invest his
portfolio for 1 or more days, then sell it at the right time to maximize his earnings. Bob
has painstakingly tracked how much his portfolio would have gained or lost for each of
the last N days. Now he has hired you to figure out what would have been the largest total gain his portfolio could have achieved.

Example :
Bob kept track of the last 10 days in the stock market. On each day, the gains/losses
are as follows: 7 -3 -10 4 2 8 -2 4 -5 -2. If Bob entered the stock market on day 4 and
exited on day 8, his gains would have been 16 (4 + 2 + 8 + -2 + 4).

Input
The input consists of integers on a line separated by spaces. The input contains N, the
number of days (0 < N < 10000), followed by N integers D (-10000 < D < 10000) indicating the gain or loss on that day.

Output
For each test case, print a line containing the maximum possible gain over the period. If no gain is possible, print 0.

Test 1
Input
10 7 -3 -10 4 2 8 -2 4 -5 -6
Expected Output
16

What about their “Expected Output”? How did they get “16”? When I call:
getBestRunProfit("10 7 -3 -10 4 2 8 -2 4 -5 -6"), I get “20”. But they said that I’m wrong and they’re right (16). They even told the recruiter that (she is less responsive to me now).

Here’s my code: https://jsbin.com/cikekev/2/edit?html,js,console

The text is poorly written/explained, and is a strange case since depending on the entry day, you might get the full output of the change on the stock, or you could get a part of it.

Though, the result is 16. Test 1, is almost identical with the one used in the explanation above. You start on day 4 and end on day 8, and end up with a total of 16.

Took a look on your code, and you need to remember that the first value in the string is the number of days, so you need to change the for loop and change “let i = 0” to “let i =1”, this will then return the desired value on this test.

Please note I have not done any more testing of your code to verify every case would be valid.

2 Likes

As information about the range of possible numbers has been clearly specified, I would ask the interviewer for further details about other unspecified information. For example, what should be done when the first number is different from the remaining number of values.

If the first number is less, should the extra numbers be ignored?
If the first number is more than the number of values, should zeros be used instead?
Or should the code warn about the mismatch and give a suitable error message?

I see that now.

The input contains N, the number of days (0 < N < 10000), 
followed by N integers D (-10000 < D < 10000)

I interpreted that as “there are less than 10000 days”. Now, your interpretation seems right. I asked for “Test 1”, how they got 16, as adding the first two numbers, 10 + 7 = 17 and 17 > 16 and furthermore, if you continue, the answer should be 20. The recruiter replied along the lines “we’ll see what the feedback is from the other candidates and if they have the same problem, we’ll ask the client.”

The recruiter showed me the other candidate’s code, which also results 20 for “Test 1”. His function (which takes an array, not a line) has the additional problem of results in negative numbers. The other candidate did not ask about why his function results “20” instead of the given expectation of “16” for “Test 1”. According to the recruiter, he was able to solve it https://jsbin.com/gorover/1/edit?js,console

I just went over this with the recruiter and pressed her to ask them:

  1. is the correct answer to “Test 1” 16 or 20?
  2. if the answer to (1) is 16, if other candidate got it right with code that results 20, please explain that inconsistency.

The only thing I changed in his code was to add the final console.log statement.
console.log(maxRangeSum([10, 7, -3, -10, 4, 2, 8, -2, 4, -5, -6]))

The first number being the number of days only adds complexity and edge cases. For something so weird, it could be better explained (and if they’d answered my question about their “Test 1” with clarity, I could have easily shifted the array in the first place).

From what you posted as the task, the answer is 16.

For the other candidate, you’re certain it is not the recruiter that say they passed? Normally, the recruiter collects a number of vetted candidates before passing them along.

The explanation is not the best, but the data set is the kind you could be expected to receive in this kind of a job, just in a fraction of the size that it is normally worked on.

The problem here could also be tied to if the recruiter has any development experience at all, or if they are just following the requirement document delivered by the client.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.