Repository navigation
Gallery Wall
codepath-wiki-review[bot] edited this page Nov 18, 2026
·
4 revisions
TIP103 Unit 2 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q:
- What is the desired outcome?
- To organize a collection of art prints into a 2D array where each row contains distinct strings and the number of rows is minimal.
- What is the desired outcome?
- Q: What is the minimal number of rows?
- A: It equals the highest number of times any single print appears, since every copy of that print needs its own row.
- Q: Does the order of rows, or of prints within a row, matter?
- A: No. Any arrangement that meets the conditions is accepted. The outputs below show the order the code produces.
- Q: Can
collectionbe empty?- A: This solution assumes at least one print. It calls
max()on the print counts, which raises an error for an empty collection.
- A: This solution assumes at least one print. It calls
HAPPY CASE
Input: collection = ["O'Keefe", "Kahlo", "Picasso", "O'Keefe", "Warhol", "Kahlo", "O'Keefe"]
Output: [["O'Keefe", "Kahlo", "Picasso", "Warhol"], ["O'Keefe", "Kahlo"], ["O'Keefe"]]
Explanation: "O'Keefe" appears 3 times, so at least 3 rows are needed. Every row holds distinct prints.
Input: collection = ["Starry Night", "Mona Lisa", "The Scream", "Guernica"]
Output: [["Starry Night", "Mona Lisa", "The Scream", "Guernica"]]
Explanation: All prints are distinct, so one row holds them all.
EDGE CASE
Input: collection = ["Mona Lisa", "Mona Lisa"]
Output: [["Mona Lisa"], ["Mona Lisa"]]
Explanation: The same print cannot appear twice in a row, so each copy gets its own row.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Count the occurrences of each print, then distribute them into the minimal number of rows such that each row contains distinct prints.
1) Count the occurrences of each print.
2) Organize prints into rows by their frequency.
3) Initialize the 2D array `rows`.
4) Distribute prints into rows while ensuring each row contains distinct prints.
5) Remove any empty rows and return the result.- Not correctly distributing prints into rows, leading to duplicate prints in a row.
def organize_exhibition(collection):
# Step 1: Manually count occurrences of each print
print_count = {}
for print_name in collection:
if print_name in print_count:
print_count[print_name] += 1
else:
print_count[print_name] = 1
# Step 2: Create a dictionary of unique prints grouped by their counts
unique_prints = {}
for print_name, count in print_count.items():
if count in unique_prints:
unique_prints[count].append(print_name)
else:
unique_prints[count] = [print_name]
# Step 3: Determine the number of rows needed
max_count = max(print_count.values())
# Step 4: Initialize the 2D array (rows) to store the organized prints
rows = [[] for _ in range(max_count)]
# Step 5: Distribute prints into rows
for count, prints in unique_prints.items():
for i in range(count):
for print_name in prints:
rows[i].append(print_name)
# Step 6: Remove any empty rows
result = [row for row in rows if row]
return result