Maximize journey earnings with prime-length forward jumps

Quick Overview

Maximize earnings on a forward journey through cities with positive or negative values. Allow adjacent moves and prime-length jumps ending in digit three, while accounting for skipped cities and negative intermediate totals.

Maximize journey earnings with prime-length forward jumps

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

A traveler starts at city 0 and must reach city `n - 1`. The array `cities` gives the money earned or spent when visiting each city. From city i, the traveler may move forward by 1 or by p positions, where p is a prime whose decimal representation ends in 3. Add the value of every visited city, including the starting and ending cities, to the journey total. Return the maximum possible journey total. Cities skipped by a jump contribute nothing, and a jump must remain within the array. ### Function Signature ```python def optimal_journey_total(cities: list[int]) -> int: ``` ### Constraints - `1 <= len(cities) <= 10000`. - `-10000 <= cities[i] <= 10000` and `cities[0] == 0`. - Movement is strictly forward. Negative intermediate totals are permitted. - All totals fit in signed 32-bit integers. ### Examples ```text Input: cities = [0, 100, 200, -500, -100, -150, -50] Output: 100 ``` Visit indices `0, 1, 2, 5, 6`, collecting `0 + 100 + 200 - 150 - 50 = 100`. The only permitted prime jump within this array is 3. ```text Input: cities = [0, -5, -2, 7] Output: 7 ``` A jump of length 3 reaches the final city directly. ```text Input: cities = [0] Output: 0 ``` The starting city is already the destination.

Overview: Maximize earnings on a forward journey through cities with positive or negative values. Allow adjacent moves and prime-length jumps ending in digit three, while accounting for skipped cities and negative intermediate totals.

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

Microsoft logo
Microsoft
Nov 16, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

A traveler starts at city 0 and must reach city n - 1. The array cities gives the money earned or spent when visiting each city. From city i, the traveler may move forward by 1 or by p positions, where p is a prime whose decimal representation ends in 3. Add the value of every visited city, including the starting and ending cities, to the journey total.

Return the maximum possible journey total. Cities skipped by a jump contribute nothing, and a jump must remain within the array.

Function Signature

def optimal_journey_total(cities: list[int]) -> int:

Constraints

  • 1 <= len(cities) <= 10000 .
  • -10000 <= cities[i] <= 10000 and cities[0] == 0 .
  • Movement is strictly forward. Negative intermediate totals are permitted.
  • All totals fit in signed 32-bit integers.

Examples

Input: cities = [0, 100, 200, -500, -100, -150, -50]
Output: 100

Visit indices 0, 1, 2, 5, 6, collecting 0 + 100 + 200 - 150 - 50 = 100. The only permitted prime jump within this array is 3.

Input: cities = [0, -5, -2, 7]
Output: 7

A jump of length 3 reaches the final city directly.

Input: cities = [0]
Output: 0

The starting city is already the destination.