r/OfferEngineering • u/Aoki_zhang • 19h ago
Coding Question Microsoft Staff SWE OA
Problem 1 — Maximum Money on a Prime-Jump Journey
A traveler goes through a row of cities given as an integer array. Each value is the money earned in that city (positive) or spent there (negative). The trip starts at city 0 and has to end at city n - 1. From any city, the traveler can either move to the next city or jump ahead p cities, where p is any prime number whose last digit is 3. The task is to return the most money the traveler can have at the end of the trip.
cities = [0, -40, 70, -300, 60, -200, -90, 30]
output = 50In this example, the array is short enough that 3 is the only usable prime ending in 3, so every move is either +1 or +3. The best route starts at city 0 and steps to city 1 (total −40). From there it jumps three cities to city 4 (−40 + 60 = 20) and jumps three more to city 7 (20 + 30 = 50).
The function to implement was optimalJourneyTotal(int cities[n]), which returns an int. The constraints were:
1 <= n <= 10^4and-10^4 <= cities[i] <= 10^4cities[0] = 0
Problem 2 — House Robber IV (LeetCode 2560)
There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he refuses to steal from adjacent homes.
The capability of the robber is the maximum amount of money he steals from one house of all the houses he robbed. You are given an integer array nums representing how much money is stashed in each house. More formally, the ith house from the left has nums[i] dollars.
You are also given an integer k, representing the minimum number of houses the robber will steal from. It is always possible to steal at least k houses. Return the minimum capability of the robber out of all the possible ways to steal at least k houses.
Example 1:
Input: nums = [2,3,5,9], k = 2
Output: 5
Explanation:
There are three ways to rob at least 2 houses:
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
Therefore, we return min(5, 9, 9) = 5.Want to practice more real interview coding questions?
You can practice real interview coding questions at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.