Min, Max, Sum & Running Totals
Aggregate a list in a single pass.
Min, Max, Sum & Running Totals is a free Coding Interview Prep lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, and your progress syncs across the web and the CoddyKit app. The Coding Interview Prep course includes 4 lessons in total.
Aggregate in One Pass
Many problems just need a single number from an array. Python's built-ins do this aggregation in one fast pass over the list.
a = [4, 1, 7, 3]Sum It Up
sum(a) adds every element and returns the total. It is C-fast, so prefer it over writing your own accumulation loop.
total = sum(a) # 15Smallest and Largest
min(a) and max(a) return the smallest and largest values. They scan the whole list once, in O(n) time.
lo, hi = min(a), max(a)Compare Two Numbers
min and max also take separate arguments. max(x, y) picks the bigger of two values without building a list first.
best = max(score, best)Aggregate with a key
Pass a key function to compare by a derived value. max(words, key=len) returns the longest word, not the lexicographic max.
longest = max(words, key=len)Guard the Empty List
min and max crash on an empty list. Pass default= or check length first to avoid a runtime error verdict.
hi = max(a, default=0)Running Totals Idea
A running total keeps a sum as you walk the array. It is the seed of prefix sums and many counting tricks.
run = 0
for x in a:
run += xTrack the Best So Far
Carry a best-so-far variable while scanning to answer max-subarray-style questions in a single pass.
best = a[0]
for x in a:
best = max(best, x)Build a Prefix List
Store every running total in a list to get a prefix array. Later you can answer range sums by subtracting two entries.
pre = [0]
for x in a:
pre.append(pre[-1] + x)accumulate Shortcut
itertools.accumulate produces running totals for you, so the prefix array is a one-liner you can trust.
from itertools import accumulate
pre = list(accumulate(a))Watch for Overflow Myths
Good news: Python integers are unbounded, so big sums never overflow. The real risk is slow code, not wrong arithmetic.
huge = sum(range(10**6)) # fineQuick Check
You need the longest word in a list. Which call is right?
Recap: One-Pass Power
You can now sum, find extremes, and carry running totals in one sweep. These aggregations turn many array problems into a single loop. ✨
Learn Coding Interview Prep with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 90
- Lessons
- 360
Frequently Asked Questions
Is the “Min, Max, Sum & Running Totals” lesson free?
Yes — the full text of “Min, Max, Sum & Running Totals” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO. The Coding Interview Prep course includes 4 lessons in total.
What will I learn in “Min, Max, Sum & Running Totals”?
Aggregate a list in a single pass. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Min, Max, Sum & Running Totals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lists, Indexing & Slicing for CP
- Build Arrays Fast with Comprehensions
- Min, Max, Sum & Running Totals
- Find the Index, Not Just the Value