Working Together to Make Python Code Scale
Let’s put efficiency to work!
Note
The Big Idea: A good solution is not just fast. It is also correct, understandable, and appropriate for the data!
Why does efficiency matter? Because a solution that works well for small inputs may become impractical for larger ones. For example, a simple search through a list of students may be fine for a single classroom, but if you have to do it for an entire school, the time taken can grow significantly.
Tip
Graded Activity Details to Follow
Important
Imagine searching a classroom attendance list once for every student in a whole school. A solution that feels instant on a tiny example can take much longer as the data grows!
Note: Friday this work will be graded as a group activity. You will have time to work together and present your solution.
The Problem:
Note
[101, 205, 101, 310, 205, 101]{101: 3, 205: 2, 310: 1}Tip
Why might this scale?
u distinct IDs.data.200_000 loans too. Does the time roughly double?Note: Timings can vary across computers and runs. This means that a single measurement is not enough to draw conclusions. A measurement is evidence, not a guarantee!
Copy this after defining your own solve and data:
solve(data) and return a result without changing data.
All groups will present their solution and timing results. Be ready to explain your approach, correctness, and efficiency!
Choose ONE of the eight provided problems to work on: Build a correct Python function, test it, and explore how its running time changes as the input grows.
Important
solve(data) and check the small examples and an edge case.Note
Tools: Python’s standard library is enough. No downloads or external datasets are needed!
Work time: about 30–50 minutes, followed by presentations on Friday
| Phase | Suggested time | What to do |
|---|---|---|
| Understand & plan | 5–10 min | Choose a problem; predict an approach |
| Build & test | 15–25 min | Code, check examples, and try an edge case |
| Measure & prepare | 10–15 min | Time two sizes and prepare your explanation |
Each person in the group must speak at the presentation.
Tip
If time is short, prioritize a correct small example and one clear timing. Then explain what you would test next!
solve, a small test, and its result.Note
Everyone: What repeated work did you avoid? What trade-off or edge case matters? If you compared methods, were they tested on the same input?
| # | Challenge | A useful idea |
|---|---|---|
| 1 | 🎮 Rank game scores | Sorting with a key |
| 2 | 📰 Count words in a message | Dictionary counting |
| 3 | 🎟️ Find guests on both lists | Set membership |
| 4 | 📦 Find repeated package IDs | Remember what you’ve seen |
| 5 | 📅 Combine busy times | Sort and sweep |
| 6 | 🌡️ Find the hottest stretch | Sliding window |
| 7 | 🚲 Find the closest race times | Sort and compare neighbors |
| 8 | 🎵 Find the most popular songs | Count and select |
Note
Get into a group of 3 to 4 people. Each group will choose a different challenge.
Note
Context: A game needs a leaderboard after a busy tournament. Each record is (name, score).
Your task: Return a new list sorted by score highest first. Break score ties by name in ascending alphabetical order. Names are unique.
💡 Hints: Explore sorted and a tuple key. How can one key express two ordering rules?
🗣️ Discuss: Why is sorting once preferable to repeatedly searching for the next highest score? What happens with tied scores?
Use the shared timing slide after defining solve. Do not change the original list.
Tip
Stretch: Explain the expected O(n log n) time of comparison sorting and the additional space used by your approach.
Note
Context: A student newspaper wants to count words in a collection of short messages.
Your task: Given a list of words, return a dictionary of counts. Treat uppercase and lowercase as the same using .lower(); each list item is already one word, with no punctuation.
💡 Hints: A dictionary can remember each word’s count. Explore dict.get or collections.Counter.
🗣️ Discuss: How do you handle capitalization? How does the number of distinct words affect memory?
Use the shared timing slide after defining solve. This input contains 1,000 distinct words, ignoring case.
Tip
Stretch: Compare counting in one pass with repeatedly using list.count for each distinct word. Which repeated work can you avoid?
Note
Context: Two campus events have guest ID lists. Organizers want to see who attended both.
Your task: data is a tuple of two lists of integer IDs. Return a sorted list of unique IDs that appear in both lists. Either list may contain duplicates.
💡 Hints: A set can remove duplicates and support fast membership checks. Remember to sort the final result.
🗣️ Discuss: How did you handle duplicates? What extra work is needed to return the IDs in order?
Use the shared timing slide after defining solve. Each list has n IDs; exactly n // 2 overlap for the even sizes above.
Tip
Stretch: Explain expected set lookup costs, the cost of sorting the overlap, and the extra space required.
Note
Context: A warehouse wants to flag IDs that appear more than once in a shipment log.
Your task: Return each repeated integer ID once, in the order in which its second occurrence is encountered.
💡 Hints: Keep track of IDs already seen and IDs already reported. A set can make each check fast.
🗣️ Discuss: Why is the order of the second occurrence important? What if an ID appears three or more times?
Use the shared timing slide after defining solve. There are 10,000 distinct IDs in each suggested input.
Tip
Stretch: Compare the expected cost of checking membership in a set with repeatedly checking a growing list.
Note
Context: A shared study room has many reservations. Combine overlapping reservations to show when the room is busy.
Your task: Given (start, end) integer pairs with start < end, return merged intervals sorted by start. Treat intervals as half-open [start, end); merge touching intervals too.
💡 Hints: Sort by start, then sweep through once while tracking the current interval’s end.
🗣️ Discuss: Why does sorting help? What happens when one interval is contained inside another?
Use the shared timing slide after defining solve. These reverse-ordered intervals overlap into one interval for n > 0.
Tip
Stretch: Identify the sorting and sweep costs separately. Does your code leave the original list unchanged?
Note
Context: A weather station wants to find the warmest consecutive stretch of k days.
Your task: data is (values, k), where values is a list of integers and 1 <= k <= len(values). Return the largest sum of any k consecutive values. Values may be negative.
💡 Hints: Calculate the first window once. When the window moves, subtract the value leaving and add the value entering.
🗣️ Discuss: What work does a sliding window avoid? Why must you test negative values?
Use the shared timing slide after defining solve. The window width is 100 days for both sizes.
Tip
Stretch: Compare summing every window from scratch with updating a running sum. What changes if k grows with n?
Note
Context: A race organizer wants to find how close the nearest two finishers were.
Your task: Given a list of at least two integer finish times, return the smallest absolute difference between two times. Equal times are allowed.
💡 Hints: Sort a copy of the times. Which pairs need to be compared after sorting?
🗣️ Discuss: Why are neighboring values enough? How does sorting compare with checking every pair?
Use the shared timing slide after defining solve. The original list should still be in its original order.
Tip
Stretch: Can you justify the time and space needed for sorting plus one pass?
Note
Context: A campus radio station wants to report its most frequently played songs.
Your task: data is (plays, k), where plays is a list of song names and k >= 0. Return up to k distinct names, ordered by play count descending, then name ascending for ties. Names are case-sensitive.
💡 Hints: Count with a dictionary or Counter, then sort by (-count, name). Consider a heap as a stretch.
🗣️ Discuss: How did you break ties? When might selecting only the top k be useful?
Use the shared timing slide after defining solve. There are 1,000 distinct songs in each suggested input.
Tip
Stretch: Compare sorting all u distinct songs with selecting k using a heap. Include counting, tie rules, and memory in your explanation.
Important
Each group has about five minutes to introduce its problem, explain its method, show working code and a test, and share measured execution times.
Note
Takeaway: Efficient programming is a process: understand, plan, test, measure, and improve. There can be more than one good solution!
Python Programming - The Efficiency Challenge