As reported: Booking.com SDE 2 (Bangalore), 2025, DSA round: find all possible ways to spend within the total budget. A tourist spends
numDaysInEachCityconsecutive days in each city; the total expenditure must not exceedtotalBudget. The input gives per-day prices for each city, and the output is the list of all possible expenditures. The same question was reported again in a later SDE 2 loop.
A tourist visits every city exactly once, staying exactly d = numDaysInEachCity consecutive days in each, one city after another. With n cities the trip lasts n × d days: the first city is visited on days 0 … d-1, the second on days d … 2d-1, and so on. Any visiting order of the cities is allowed.
pricesPerCity[c][day] is the price of staying in city c on that day of the trip (each row has n × d entries). The cost of a visiting order is the sum of the prices of the days spent in each city.
Return every distinct total cost that does not exceed totalBudget, sorted in increasing order.
Example 1
numDaysInEachCity = 2, totalBudget = 390
pricesPerCity = [
[10,20,50,80,100,20], // Paris
[90,10,40,90,12,11], // LA
[70,20,10,30,20,80] // Amsterdam
]
Output: [93,243,260,330,340]
Paris on days 1–2 (10 + 20 = 30), LA on days 3–4 (40 + 90 = 130), Amsterdam on days 5–6 (20 + 80 = 100) costs 260 ≤ 390, so 260 is included. The six orders cost 260, 93, 330, 260, 243 and 340; 260 appears twice but is listed once.
Example 2
numDaysInEachCity = 2, totalBudget = 250, pricesPerCity as in Example 1
Output: [93,243]
Example 3
numDaysInEachCity = 2, totalBudget = 50, pricesPerCity as in Example 1
Output: []
Constraints: 1 <= n <= 8 cities, 1 <= numDaysInEachCity <= 30, pricesPerCity[c].length == n × numDaysInEachCity, 0 <= pricesPerCity[c][day] <= 10^4, 0 <= totalBudget <= 10^9.
Code runner format: one JSON value per line: numDaysInEachCity, totalBudget, pricesPerCity. Implement allExpenditures(numDaysInEachCity, totalBudget, pricesPerCity) returning the sorted list of distinct totals.
Clarifications
- Every city is visited exactly once and the stays are back to back (no free days).
- "Does not exceed" means
total <= totalBudget. - Two orders with the same total contribute one entry.