FastPrepMaximum Total of Nonadjacent Ratings
Problems/Maximum Total of Nonadjacent Ratings

Maximum Total of Nonadjacent Ratings

Morgan Stanley logoMorgan Stanley● EasyFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The judged task matches the reported rating-selection task at about 90%. The author says selecting a rating prevents selecting its adjacent ratings and names the House Robber pattern. This practice interprets adjacency by array position, not numeric value. Nonnegative bounds, optional empty selection, empty input and callable interface are authored assumptions.

Choose zero or more indices from ratings so that no two selected indices are consecutive. Return the maximum sum of the selected ratings.

For this exercise, adjacency is by array position: selecting index i prevents selecting i-1 and i+1 when they exist. Equal or consecutive numeric rating values do not create additional exclusions. The first and last positions are not neighbors unless the array has length 2.

Ratings are nonnegative. Choosing no indices is allowed and gives 0; an empty array therefore returns 0. Return only the maximum total and leave ratings unchanged.

Function

maxNonAdjacentRatings(ratings: int[]) → int

Examples

Example 1

ratings = [2,7,9,3,1]return = 12

Select indices 0,2,4 with values 2,9,1 for total 12. They are pairwise nonconsecutive; no allowed selection has a higher total.

Example 2

ratings = [5,1,1,5]return = 10

Select the two endpoints, indices 0 and 3, for 5+5=10. This is a linear array, so the endpoints are not adjacent.

Constraints

  • For this exercise, assume 0 <= ratings.length <= 1000.
  • Every rating is between 0 and 1000000, inclusive.

More Morgan Stanley problems

  • Unique Grid PathsPHONE SCREEN · Seen Oct 2026
  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Three-Resource 0/1 KnapsackOA · Seen Sep 2026
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int maxNonAdjacentRatings(int[] ratings) {
        // Write your code here.
    }

}
Checking account…