Quick Overview

Choose a maximum-sum subsequence of positive or negative movie ratings while ensuring no two consecutive positions are skipped. At least one movie must remain, original order is preserved, and the solution must scale to one million entries.

Maximize Movie Ratings Without Skipping Two in a Row

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem An array contains positive or negative movie ratings in viewing order. Choose a subsequence with maximum total rating subject to one rule: you may not skip two consecutive movies. Return the maximum attainable sum. ### Function Contract Implement `maxRatingSum(ratings)`. ### Constraints & Assumptions - `1 <= len(ratings) <= 1,000,000`. - `-10^9 <= ratings[i] <= 10^9`. - At least one movie must be selected. - Skipping the first or last movie is allowed, but no adjacent pair of positions may both be skipped. - Use a wide integer type for the result. ### Clarifying Questions to Ask - Must selected movies be contiguous? No; they form a subsequence. - Can every rating be negative? Yes. - Does “cannot skip more than one consecutive movie” mean each adjacent pair contains a selected movie? Yes. - Is selecting no movies allowed? No. ```hint Track the previous choice At position `i`, the best state depends on whether movie `i` is selected or skipped; a skip is legal only after a selection. ``` ```hint Preserve negative states Do not initialize an impossible state to zero if that would accidentally allow an empty selection to beat every negative valid choice. ``` ### Examples ```text ratings = [5, -2, 4] output = 9 ratings = [-5, -1, -4] output = -1 ``` In the second example, selecting only the middle movie skips one movie on each side but never skips two adjacent positions. ### Evaluation Focus - Applies the rule to leading, internal, and trailing skips. - Handles all-negative input without choosing an invalid empty subsequence. - Uses constant auxiliary state rather than a full table. - Runs in `O(n)` time. ### Extensions to Discuss 1. How would you reconstruct the selected indices? 2. What changes if up to `k` consecutive movies may be skipped? 3. How would a requirement to select the first and last movie simplify the states?

Overview: Choose a maximum-sum subsequence of positive or negative movie ratings while ensuring no two consecutive positions are skipped. At least one movie must remain, original order is preserved, and the solution must scale to one million entries.

Choose a nonempty subsequence of ratings with maximum sum while preserving order and never skipping two adjacent movies. The first or last movie may be skipped. Return the maximum using a wide integer type.

Constraints

  • 1 <= len(ratings) <= 1,000,000.
  • Each rating is between -1,000,000,000 and 1,000,000,000.
  • At least one movie is selected and no adjacent pair is skipped.

Examples

Input: ([5,-2,4],)

Expected Output: 9

Explanation: Positive example.

Input: ([-5,-1,-4],)

Expected Output: -1

Explanation: All-negative example.

Hints

  1. Track whether the previous position was selected.
  2. Preserve negative valid states.

Loading coding console...