Courses / Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course
2.1 Evaluating Computational Solutions: Efficiency, Clarity and Correctness
Unit 2 – Computational Thinking and Algorithms · 80 min read · 9 MCQs to check yourself
Big O notation explained visually – time complexity for beginners
1. Three questions to ask about any solution
| Criterion | Question | How we check it |
|---|---|---|
| Correctness | Does it give the right answer for every valid input? | Test cases, edge cases, trace tables, reasoning |
| Efficiency | How much time (steps) and memory does it use as the input grows? | Count steps, Big-O, timing |
| Clarity | Can another programmer read, understand and change it easily? | Meaningful names, small functions, comments, consistent layout |
A solution that is fast but wrong is useless; a correct solution that takes hours for 1 million records is impractical; a clever solution that nobody can read is hard to maintain. Good solutions balance all three.
2. Efficiency – time and space complexity
We count basic steps (comparisons, additions) as a function of the input size n. Big-O notation describes how that count grows for large n, ignoring constants.
| Big-O | Name | Example | Steps for n = 1,000,000 |
|---|---|---|---|
| O(1) | constant | access list[5], formula n(n+1)/2 |
1 |
| O(log n) | logarithmic | binary search | about 20 |
| O(n) | linear | linear search, summing a list | 1,000,000 |
| O(n log n) | linearithmic | merge sort | about 20,000,000 |
| O(n²) | quadratic | bubble sort, comparing every pair | 1,000,000,000,000 |
Example – two correct solutions, very different efficiency
Sum of 1 + 2 + … + n:
def sum_loop(n): # O(n): n additions
total = 0
for i in range(1, n + 1):
total += i
return total
def sum_formula(n): # O(1): one calculation
return n * (n + 1) // 2
print(sum_loop(100), sum_formula(100))
print(sum_loop(1000000) == sum_formula(1000000))
Output:
5050 5050
True
Both are correct; the formula is far more efficient.
Space complexity is the extra memory used. Reversing a list by building a new list uses O(n) extra space; swapping in place uses O(1).
3. Clarity – code for humans
Compare:
def f(a):
x = 0
for i in a:
if i % 2 == 0: x += 1
return x
def count_even(numbers):
"""Return how many numbers in the list are even."""
even_count = 0
for number in numbers:
if number % 2 == 0:
even_count += 1
return even_count
print(f([4, 7, 10, 3]), count_even([4, 7, 10, 3]))
Output:
2 2
Same result and same efficiency – but count_even is clearer: meaningful names, a docstring, one job per function. Clarity reduces bugs and makes future changes cheaper.
Clarity checklist: meaningful names · short functions with one task · comments explain why · consistent indentation · no repeated code (use functions) · avoid “clever” tricks.
4. Correctness – does it always work?
- Normal cases: typical inputs (marks 45, 78).
- Boundary/edge cases: smallest/largest values, empty list, one item, zero, negative numbers, duplicates.
- Invalid inputs: text instead of numbers – handled with validation.
- Trace table: follow variables line by line by hand.
Example – finding the maximum:
def find_max(values):
largest = 0 # BUG: wrong if all values are negative
for v in values:
if v > largest:
largest = v
return largest
def find_max_fixed(values):
largest = values[0] # start from the first real value
for v in values[1:]:
if v > largest:
largest = v
return largest
print(find_max([4, 9, 2]), find_max([-5, -2, -9]))
print(find_max_fixed([4, 9, 2]), find_max_fixed([-5, -2, -9]))
Output:
9 0
9 -2
The first version passes the normal test but fails the edge case of all-negative numbers – it returns 0, which is not even in the list.
Trace table for find_max_fixed([4, 9, 2]):
| step | v | v > largest? | largest |
|---|---|---|---|
| start | – | – | 4 |
| 1 | 9 | yes | 9 |
| 2 | 2 | no | 9 |
| return | 9 |
5. Assessing the effectiveness of a computational solution
- Does it solve the real problem (meets requirements)?
- Is it correct for all test cases, including edge cases?
- Is it efficient enough for the expected data size (Big-O, timing)?
- Is it clear and maintainable?
- Is it usable, secure and robust (handles errors, does not crash)?
Short and long questions
1. Two students write programs to check if a number is in a list of 1,000,000 student IDs. Ali uses linear search on the unsorted list; Sara sorts the list once and then uses binary search. Evaluate both solutions in terms of efficiency, clarity and correctness, and recommend one for a system that performs thousands of searches per minute. (6)
Model answer
Efficiency (2): Ali – linear search is O(n): up to 1,000,000 comparisons per search. Sara – sorting once costs O(n log n) (about 20 million steps), but each binary search is O(log n): about 20 comparisons. For thousands of searches per minute Sara’s total work is far smaller after the first sort.
Clarity (2): linear search is simpler and very easy to read; binary search needs careful handling of low/high/mid but is still short. Both can be made clear with good names and comments.
Correctness (1): both are correct if Sara’s list stays sorted. Binary search gives wrong results on unsorted data, and new IDs must be inserted in order. Edge cases (empty list, ID not present, first/last element) must be tested for both.
Recommendation (1): use Sara’s approach (sorted list + binary search), or a dictionary/set (O(1) average lookups) for even faster checks, because efficiency matters most at this scale.
2. Why is clarity important in a computational solution? Give three practices that improve it.
Model answer
Clear code is easier to read, debug, test and change, especially in teams, reducing errors and maintenance cost. Practices: (1) meaningful variable and function names, (2) small functions doing one task with docstrings/comments explaining why, (3) consistent indentation and avoiding repeated code.
3. What is an edge case? Give three edge cases for a function that returns the average of a list of marks.
Model answer
An edge (boundary) case is an input at the extreme or unusual limit of valid data where programs often fail. For an average function: (1) an empty list (division by zero), (2) a list with one mark, (3) marks at the limits such as 0 and 100, or negative/invalid values.
4. Write two Python functions to find the sum of 1 to n – one using a loop and one using a formula. Which is more efficient and why?
Model answer
def sum_loop(n):
total = 0
for i in range(1, n + 1):
total += i
return total
def sum_formula(n):
return n * (n + 1) // 2
The formula is more efficient: O(1) – one calculation for any n – while the loop is O(n), doing n additions. Both are correct.
5. Differentiate between time complexity and space complexity with one example each.
Model answer
- Time complexity: how the number of steps grows with input size n. Example: linear search is O(n); binary search is O(log n).
- Space complexity: how much extra memory is needed as n grows. Example: copying a list into a new reversed list needs O(n) extra space; swapping elements in place needs O(1).
6. How do we assess the effectiveness of a computational solution?
Model answer
Check it against these criteria:
- Correctness – gives the right output for all valid inputs, tested with normal, boundary and invalid cases (trace tables, test cases).
- Efficiency – uses reasonable time and memory as input grows (step counts, Big-O, timing).
- Clarity / maintainability – readable names, small functions, comments, so others can understand and modify it. (Also: meets user requirements, handles errors robustly.)
This lesson ends with a quiz of 9 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Unit 1 Review, Situational Quiz and Unit Test · Next: 2.2 Data Structures: Lists, Arrays, Stacks and Queues
All lessons in Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course