TIP102 Unit 7 Session 1 Advanced (Click for link to problem statements)
The deli staff is in desperate need of caffeine to keep them going through their shift and has decided to divide the coffee supply equally among themselves. Each batch of coffee is stored in containers of different sizes. Write a recursive function can_split_coffee()
that accepts a list of integers coffee
representing the volume of each batch of coffee and returns True
if the coffee can be split evenly by volume among n
staff and False
otherwise.
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
n
staff members using recursion.n
?
False
.HAPPY CASE
Input: [4, 4, 8], 2
Output: True
Explanation: The total volume is 16, which can be split into two equal parts of 8.
Input: [5, 10, 15], 4
Output: False
Explanation: The total volume is 30, which cannot be split into four equal parts.
EDGE CASE
Input: [7, 3, 2], 2
Output: True
Explanation: The total volume is 12, which can be split into two equal parts of 6.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Partitioning a Set, we want to consider the following approaches:
n
subsets each with a sum equal to the total sum divided by n
.Plan the solution with appropriate visualizations and pseudocode.
General Idea:
n
. If not, return False
.n
subsets, each with a sum equal to total_volume // n
.Recursive Approach:
1) Calculate the `total_volume` of the coffee list.
2) If `total_volume` is not divisible by `n`, return `False`.
3) Define a helper function `can_divide(coffee, n, target, current_sum)`:
a) Base case: If `n` is 0, return `True` (all staff members have received their share).
b) If `current_sum` equals `target`, start partitioning for the next staff member.
c) If the coffee list is empty, return `False`.
d) Recursively try to include or exclude the first coffee batch in the current partition.
4) Return the result of `can_divide(coffee, n, target, 0)`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def can_split_coffee(coffee, n):
total_volume = sum(coffee)
# If the total volume isn't divisible by n, return False
if total_volume % n != 0:
return False
target = total_volume // n
return can_divide(coffee, n, target, 0)
def can_divide(coffee, n, target, current_sum):
if n == 0:
return True # If we've successfully partitioned for all staff
if current_sum == target: # Current staff member has a full share
return can_divide(coffee, n - 1, target, 0) # Move to the next staff member
if not coffee:
return False # No more coffee batches to partition
# Try including the first batch of coffee in the current partition
include = can_divide(coffee[1:], n, target, current_sum + coffee[0])
# Try excluding the first batch of coffee from the current partition
exclude = can_divide(coffee[1:], n, target, current_sum)
return include or exclude
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
can_split_coffee
function with the input [4, 4, 8]
and 2
. The function should return True
after partitioning the coffee into two equal parts.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(2^N)
in the worst case due to the recursive exploration of all subsets, where N
is the number of coffee batches. The problem is similar to the subset sum problem, which is NP-complete.O(N)
due to the recursion stack, where N
is the depth of recursion proportional to the number of coffee batches.