Magicsheet logo

Maximum Vacation Days

Hard
68.7%
Updated 6/1/2025

Maximum Vacation Days

What is this problem about?

The "Maximum Vacation Days" coding problem challenges you to find the maximum number of vacation days you can take over a period, typically a set number of weeks. You are given a schedule of flights between various cities, including the number of vacation days available in each city for each week. The constraints usually involve being able to fly from your current city to another, or staying in the same city, for each subsequent week. This problem is a classic application of dynamic programming on a graph-like structure, where cities are nodes and flights are edges. It asks you to make optimal choices weekly to maximize your total vacation days, considering both travel and local vacation opportunities.

Why is this asked in interviews?

This Maximum Vacation Days interview question is frequently asked by companies like Meta and Google to assess a candidate's proficiency in dynamic programming, especially in scenarios involving state transitions over time or across interconnected entities. It evaluates your ability to define a clear DP state (e.g., dp[week][city]), formulate recurrence relations, and handle array and matrix-based inputs. Interviewers are keen to see how you optimize the solution for time and space complexity, particularly when dealing with a potentially large number of cities and weeks. It's a robust problem that demonstrates systematic thinking and the ability to apply powerful algorithmic patterns to real-world scheduling problems.

Algorithmic pattern used

The primary algorithmic pattern for solving the "Maximum Vacation Days" problem is Dynamic Programming. A typical DP approach involves defining dp[i][j] as the maximum vacation days one can get starting from week i and ending in city j. The recurrence relation would then consider two main choices for each week:

  1. Staying in the current city j and adding the vacation days available in city j for week i.
  2. Flying from a previous city k to city j (if a flight exists) and adding the vacation days available in city j for week i. The transition would look for the maximum among all possible previous cities k. The base case would be for the last week. The solution can also be formulated bottom-up, starting from week 0 and building up to the final week. This matrix and dynamic programming interview pattern allows for an efficient solution by avoiding re-computation of subproblems.

Example explanation

Imagine 2 cities (City 0, City 1) and 2 weeks. flights matrix (flights[i][j] is 1 if flight from i to j exists, 0 otherwise): [[0, 1], [1, 0]] (City 0 to 1, City 1 to 0) days matrix (days[i][j] = vacation days in city j for week i): [[10, 5], [12, 8]] (Week 0: 10 days in City 0, 5 in City 1; Week 1: 12 days in City 0, 8 in City 1)

Let dp[w][c] be max vacation days up to week w, ending in city c.

Base Case (Week 0):

  • dp[0][0] = days[0][0] = 10 (Start in City 0, stay)
  • dp[0][1] = days[0][1] = 5 (Start in City 0, fly to City 1 if possible - in this case, yes)

Week 1:

  • dp[1][0] (ending in City 0):
    • From City 0 in Week 0 (dp[0][0]): Stay in City 0. dp[0][0] + days[1][0] = 10 + 12 = 22.
    • From City 1 in Week 0 (dp[0][1]): Fly from City 1 to City 0. dp[0][1] + days[1][0] = 5 + 12 = 17.
    • dp[1][0] = max(22, 17) = 22.
  • dp[1][1] (ending in City 1):
    • From City 0 in Week 0 (dp[0][0]): Fly from City 0 to City 1. dp[0][0] + days[1][1] = 10 + 8 = 18.
    • From City 1 in Week 0 (dp[0][1]): Stay in City 1. dp[0][1] + days[1][1] = 5 + 8 = 13.
    • dp[1][1] = max(18, 13) = 18.

Max vacation days is max(22, 18) = 22. This shows how the Maximum Vacation Days coding problem uses DP.

Common mistakes candidates make

A frequent mistake in the Maximum Vacation Days coding problem is mismanaging the state transitions, particularly when considering flights between cities. Candidates might incorrectly assume a flight always exists or fail to account for the option of staying in the current city. Another common error is using a greedy approach, simply choosing the city with the most vacation days each week, which does not guarantee a global maximum due to dependencies on future weeks and flight availability. Incorrectly handling base cases or the boundary conditions of the weeks and cities can also lead to wrong results. Overlooking the possibility of unreachable cities or flight paths is another pitfall, causing errors in the dynamic programming logic.

Interview preparation tip

To master the Maximum Vacation Days interview question, dedicate time to understanding dynamic programming problems on graphs or grid structures. Practice defining your DP state clearly, considering all relevant parameters (like current week and city). Formulate the recurrence relation meticulously, ensuring it covers all possible transitions (staying, flying). Pay close attention to the order of computation in your DP table to ensure that dependencies are met. Work through small examples by hand to verify your logic. This array, matrix, and dynamic programming interview pattern is fundamental for many complex scheduling and optimization problems.

Similar Questions