KatInterview

Smallest Venue Set That Fits a Group

Dynamic ProgrammingAuto-graded · 20 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingsubset-sumcombinatorics

Last updated

Tests capacity-constrained subset selection with multi-level tie-breaking: fewest venues, then minimum total capacity, then lexicographically smallest ID list.

Problem

You are managing a booking platform. Each venue is described by a dictionary with three keys:

  • "id" — an integer identifier
  • "neighborhood" — a string indicating which part of the city the venue is located in
  • "capacity" — a positive integer for the number of guests the venue can accommodate

Given a target_neighborhood (string) and a group_size (int), find the smallest possible set of venues drawn exclusively from target_neighborhood whose combined capacity is at least group_size.

Selection rules

  1. Feasibility — the combined capacity of the chosen venues must be ≥ group_size.
  2. Minimize count — among all feasible sets, choose one with the fewest venues.
  3. Minimize total — if several sets tie on count, prefer the one whose combined capacity is smallest (i.e., waste as few slots as possible).
  4. Lexicographic fallback — if sets still tie on both count and total, choose the one whose sorted list of IDs is lexicographically smallest.

Return the chosen IDs as an ascending list of integers. Return an empty list if no feasible set exists or if group_size ≤ 0.

Constraints

  • 1 ≤ len(venues) ≤ 20
  • 1 ≤ capacity ≤ 1000 for each venue
  • IDs are positive integers; assume they are unique within the input
  • group_size may be any integer (including non-positive values)
  • Venues outside target_neighborhood must be ignored entirely
  • Neighborhood strings are case-sensitive

Examples

Example 1

Input: smallest_venue_set([{'id': 1, 'neighborhood': 'downtown', 'capacity': 5}, {'id': 2, 'neighborhood': 'downtown', 'capacity': 3}, {'id': 3, 'neighborhood': 'downtown', 'capacity': 1}, {'id': 4, 'neighborhood': 'uptown', 'capacity': 4}, {'id': 5, 'neighborhood': 'uptown', 'capacity': 2}], 'downtown', 6)

Output: [1, 3]

Example 2

Input: smallest_venue_set([{'id': 1, 'neighborhood': 'downtown', 'capacity': 5}, {'id': 2, 'neighborhood': 'downtown', 'capacity': 3}, {'id': 3, 'neighborhood': 'downtown', 'capacity': 1}, {'id': 4, 'neighborhood': 'uptown', 'capacity': 4}, {'id': 5, 'neighborhood': 'uptown', 'capacity': 2}], 'downtown', 3)

Output: [2]

Example 3

Input: smallest_venue_set([{'id': 1, 'neighborhood': 'downtown', 'capacity': 5}, {'id': 2, 'neighborhood': 'downtown', 'capacity': 3}, {'id': 3, 'neighborhood': 'downtown', 'capacity': 1}, {'id': 4, 'neighborhood': 'uptown', 'capacity': 4}, {'id': 5, 'neighborhood': 'uptown', 'capacity': 2}], 'downtown', 16)

Output: []

Example 4

Input: smallest_venue_set([{'id': 1, 'neighborhood': 'downtown', 'capacity': 5}, {'id': 2, 'neighborhood': 'downtown', 'capacity': 3}, {'id': 3, 'neighborhood': 'downtown', 'capacity': 1}], 'downtown', 10)

Output: []

Hints

Hint 1

Start by filtering out every venue not in target_neighborhood — they can never appear in the answer. Then think about what the minimum feasible count must be: if you sort filtered capacities descending and greedily accumulate, you get the smallest possible k quickly. Does that greedy approach also nail down which specific venues to pick?

Hint 2

Greedy gives you k but not the right subset — there can be many size-k subsets that all reach group_size. To pick the one with the smallest total, you need to explore all reachable sums exactly. A 0/1 knapsack-style DP over exact sums works: keep, for every reachable total, the best subset (fewest items, then lex-smallest IDs) that achieves it.

Hint 3

Once the DP is built, scan every entry whose total ≥ group_size and pick the winner by comparing (count, total, sorted_id_tuple). Be careful to iterate over a snapshot of the DP table when processing each venue — updating in place would let a single venue be used more than once.