Quick Overview

Maximize fixed-step game scores with backward dynamic programming, preserving mandatory jumps, negative totals, and the distinction from optional early stopping.

Maximize a Fixed-Step Game Score by Choosing the Start

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

Given integer array `game_val` and a positive jump size k, choose a starting index and repeatedly advance by exactly k while the next index remains in bounds. The score is the sum of visited values, including the start. Return the maximum score over starting positions. For `[2,-3,4,6,1]` and k=2, starting at index 0 visits 0,2,4 for score 7. ### Constraints & Assumptions Assume nonempty input and mandatory continuation until the next jump leaves the array. The source does not explicitly discuss early stopping; allowing it would change the recurrence on negative suffixes. ### Clarifying Questions to Ask - Must the player continue whenever the next jump is in bounds? - Is k always positive, and what should empty input return? ```hint Solve later starting positions first The score starting at i contains the entire score starting at i+k, if that index exists. ``` ### What a Strong Answer Covers - A backward recurrence for each starting position. - Correct negative-value handling without allowing an empty path. - Linear time and an explanation of the early-stopping variant. ### Follow-up Questions - How would optional early stopping change the recurrence? - How can scores be accumulated one residue class modulo k at a time?

Overview: Maximize fixed-step game scores with backward dynamic programming, preserving mandatory jumps, negative totals, and the distinction from optional early stopping.

Read the full Stripe Software Engineer interview experience this question came from

Choose any starting index in the nonempty integer array `game_val`. Include its value in your score, then repeatedly advance by exactly `k` whenever the next index remains in bounds. Include every visited value, including negative ones. Return the maximum score over all possible starting indices.

Constraints

  • The array contains 1 through 2^31 − 1 entries.
  • Every value lies between −(2^53 − 1) and 2^53 − 1 inclusive. The step k lies between 1 and 2^53 − 1 inclusive.
  • The maximum score fits in the same signed range as the input values. Other path sums may fall outside that range.

Examples

Input: ([2, -3, 4, 6, 1], 2)

Expected Output: 7

Explanation: Starting at index 0 gives 2 + 4 + 1, the best score.

Input: ([9, -20, 5], 1)

Expected Output: 5

Explanation: Starting at 0 requires taking the negative suffix. Starting at 2 alone scores 5.

Hints

  1. A starting position determines the entire path because every in-bounds jump is required.

Maximum Fixed-Step Score

hardStripeSoftware Engineer

Choose any starting index in the nonempty integer array game_val. Include its value in your score, then repeatedly advance by exactly k whenever the next index remains in bounds. Include every visited value, including negative ones. Return the maximum score over all possible starting indices.

Examples

Example 1
Input
([2, -3, 4, 6, 1], 2)
Output
7
Notes
Starting at index 0 gives 2 + 4 + 1, the best score.
Example 2
Input
([9, -20, 5], 1)
Output
5
Notes
Starting at 0 requires taking the negative suffix. Starting at 2 alone scores 5.

Constraints

  • •The array contains 1 through 2^31 − 1 entries.
  • •Every value lies between −(2^53 − 1) and 2^53 − 1 inclusive. The step k lies between 1 and 2^53 − 1 inclusive.
  • •The maximum score fits in the same signed range as the input values. Other path sums may fall outside that range.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def maximum_fixed_step_score(game_val: list[int], k: int) -> int:
Sample call
maximum_fixed_step_score([2, -3, 4, 6, 1], 2)
Language
Case 1
Input
([2, -3, 4, 6, 1], 2)
Expected
7
Case 2
Input
([9, -20, 5], 1)
Expected
5

Browse More Questions

Show the approach

Approach

The score at i is its value plus the score at i+k when that index exists. The last min(n,k) indices are the final positions of all possible step-k chains. For each chain, begin at its final position and move backward by k, accumulating a running sum. After each addition the sum is the exact score of the path beginning there. Every starting position lies on exactly one chain, so the largest observed sum is the answer. Keep a nonempty path as the initial best and use exact arithmetic on the running sums.

Time complexity:
O(n)
Space complexity:
O(1) auxiliary values.