DSA Intro
A coding interview is a timed test where a candidate writes code and explains its cost. This series teaches problem patterns first and treats cost as part of the score.
Two ways to add numbers
A loop adds the numbers one at a time. The built-in sum does the same job in one call.
numbers = [4, 8, 15, 16, 23]
total = 0
for n in numbers:
total += n
print(total)
print(sum(numbers))Counting the loop's steps
A step is one small piece of work, like adding one number. This counter records one step per number.
numbers = [4, 8, 15, 16, 23]
steps = 0
total = 0
for n in numbers:
total += n
steps += 1
print(total, steps)Example
numbers = [12, 7, 25, 3, 18, 9]
loop_total = 0
steps = 0
for n in numbers:
loop_total += n
steps += 1
builtin_total = sum(numbers)
print("Loop total:", loop_total)
print("Built-in total:", builtin_total)
print("Loop steps:", steps)Key points
- Pattern: a reusable shape of solution that fits many problems
- Cost: the count of steps and extra memory a solution spends
- A coding interview grades the chosen pattern and the stated cost, not only the final answer
- This course counts steps inside the code, so every cost claim appears as a printed number
In this example, a loop adds the six numbers one at a time. A counter gains one for each addition and ends at six steps. The built-in sum then adds the same list in one call. Both totals print as 74, so the two ways agree. The counted steps are the kind of cost statement an interview answer needs.
Note
Every program in this course uses plain Python and no extra libraries.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· Python Playground
Exercise
What does a coding interview test besides a working answer?