My first instinct was just standard DP, which is right, but I fumbled the setup because I didn't precompute the valid primes ending in 3 before writing the recurrence.
Model the problem as a dynamic programming problem where dp[i] represents the maximum sum to reach index i. Precompute all valid jump lengths (primes ending in 3) up to n, then for each index, consider all possible previous positions from which you can jump to it, and take the maximum. Return dp[n-1].
Pro tip: Clarify with the interviewer whether you can overshoot the last index or must land exactly on it, as this affects the DP transitions. Also, mention that you can optimize by only iterating over valid jump lengths rather than all indices, reducing time complexity.
Restate the problem to ensure clarity: you start at index 0, can jump 1 or a prime ending in 3, and want to maximize the sum of visited cells including start and end. Ask about edge cases like negative values, array size, and whether jumps must land exactly on the last index.
Generate all prime numbers ending in 3 up to n-1 (the maximum possible jump length). Use a sieve or simple primality check, and store them in a list for efficient lookup.
Let dp[i] be the maximum sum to reach index i. Initialize dp[0] = arr[0] and others to -infinity. For each i from 1 to n-1, dp[i] = arr[i] + max(dp[i-1], max over valid jumps j where i-j is a valid jump length of dp[i-j]).
If dp[i] remains -infinity, it means index i is unreachable. After filling the DP table, return dp[n-1] if reachable, else indicate no valid path (or return -infinity).
Time complexity is O(n * k) where k is the number of valid jump lengths (about n / log n). Space complexity is O(n). Mention that you can optimize space to O(max jump length) if only the last few states are needed, but O(n) is fine.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.