Cheapest Mix of Day, Week, and Month Transit Passes for a Commute

Last updated: November 29, 2026

Quick Overview

Given the sorted calendar days of the year on which a Google employee will commute, and prices for 1-day, 7-day and 30-day transit passes, return the minimum total cost to cover every commute day.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given the sorted calendar days of the year on which a Google employee will commute, and prices for 1-day, 7-day and 30-day transit passes, return the minimum total cost to cover every commute day.

An onsite favorite because a natural greedy fails and the DP index can be either calendar days or travel days. Google interviewers usually ask for both versions and for the trade-off between them.

What the Interviewer Expects
  • Define cost[d] as the cheapest coverage of all travel days up to day d
  • Carry cost forward on non-travel days
  • Consider all three passes ending today
  • Offer the travel-day-indexed version using pointers
  • Handle prices where a longer pass is cheaper than a shorter one
Key Topics to Cover
1D dynamic programming
Interval coverage
Minimum cost tickets
Two pointers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • The year is replaced by a horizon of 10^9 days with only 10^5 travel days.
  • Add a pass that covers only weekdays.
  • Return which passes to buy and when.
  • Passes have different prices in different months.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: sorted list days of distinct integers in 1..365 and costs = [c1, c7, c30]. A pass bought on day d covers days d through d + L - 1. Output the minimum cost so every day in days is covered....

High-Level Design

Index by calendar day. Let cost[d] be the minimum spend to cover every travel day up to and including d. If d is not a travel day, cost[d] = cost[d-1]. Otherwise one of the passes must end on or a...


Submit Your Answer
Markdown supported