CSS NEXUS
CSS Solved Computer Science

CSS 2026Computer SciencePaper I, Q. 5

(a) Why is Binary Search not suitable for an unsorted list? (6) (b) What is the difference between Time Complexity and Space Complexity? Explain with examples. (6) (c) Write an algorithm with an example showing how Stack can be used to evaluate a postfix expression. (8)

By CSS Nexus Team ·

Original CSS 2026 Computer Science I paper Original CSS 2026 Computer Science II paper Original CSS 2016–2026 Computer Science paper

This answer to the CSS 2026 Computer Science paper (Paper I, Q. 5) was written by the CSS Nexus team. Read the outline first, then see how each point is developed.

Outline

  • 1. Introduction
  • 2. Part (a): why binary search fails on an unsorted list
  • ✓How binary search works; the sorted-order invariant; a counter-example; linear search or sort first
  • 3. Part (b): time complexity vs space complexity
  • ✓Definitions; Big-O; examples (linear search, merge sort vs heap sort, Fibonacci); trade-off; comparison table
  • 4. Part (c): evaluating a postfix expression with a stack
  • ✓Why postfix; algorithm; worked example with trace; complexity; error cases
  • 5. Conclusion

Answer

Introduction

The Swiss computer scientist Niklaus Wirth titled his classic textbook with an equation: Algorithms + Data Structures = Programs (Prentice-Hall, 1976). Each part of this question illustrates that equation. Binary search is an algorithm that works only on a particular arrangement of data; complexity analysis measures what an algorithm costs in time and memory; and postfix evaluation shows how a simple data structure, the stack, makes an otherwise awkward problem straightforward.

Algorithms + Data Structures

  1. Binary search needs sorted data
  2. Complexity measures cost
  3. Stack evaluates postfix
  4. Efficient programs

Part (a): Why binary search is not suitable for an unsorted list

1. How binary search works.

Binary search compares the target with the middle element. If they are equal, the search ends; if the target is smaller, it continues in the left half; if larger, in the right half. Each step discards half of the remaining elements, so it needs at most about log₂n comparisons, O(log n). (Reference: Donald Knuth, The Art of Computer Programming, Vol. 3, Sorting and Searching, 2nd ed., Addison-Wesley, 1998, Section 6.2.1.)

2. It depends on the sorted-order invariant.

Discarding the left half is valid only if every element there is smaller than the middle element, and discarding the right half only if every element there is larger. That is true only when the list is sorted. In an unsorted list, the target may lie in the half that is thrown away.

3. A counter-example.

Search for 3 in the unsorted array [8, 1, 10, 6, 3, 4, 9] (indices 0-6), where 3 is at index 4.

- low = 0, high = 6, mid = 3: A[3] = 6. Since 3 < 6, search the left half, indices 0-2.

- low = 0, high = 2, mid = 1: A[1] = 1. Since 3 > 1, search indices 2-2.

- mid = 2: A[2] = 10. Since 3 < 10, high becomes 1, which is less than low, so the search ends: "not found".

The answer is wrong, because 3 was in the right half that was discarded at the first step.

4. What to do instead.

For an unsorted list, use linear search, O(n), which checks every element and is always correct. If many searches will be made on the same data, it pays to sort once, O(n log n), and then use binary search, O(log n), for each query; alternatively, use a hash table for average O(1) look-ups. Binary search also needs random access, so it is unsuitable even for a sorted linked list. (Reference: Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022.)

Binary search decision

  1. 1

    Compare target with middle

  2. 2

    Equal: found

  3. 3

    Smaller: keep left half

  4. 4

    Larger: keep right half

  5. 5

    Valid only if the list is sorted

Part (b): Time complexity vs space complexity

Time complexity is the amount of computation an algorithm needs, expressed as a function of the input size n, usually by counting basic operations. Space complexity is the amount of memory it needs as a function of n, including auxiliary storage such as extra arrays and the recursion stack, in addition to the input itself. Both are normally stated in Big-O notation, which gives an upper bound on growth for large n and ignores constants. (Reference: Cormen et al., 2022, Chapter 3.)

Example 1: linear search.

Searching an array of n elements one by one takes up to n comparisons, so its time complexity is O(n). It needs only an index variable, so its space complexity is O(1).

Example 2: merge sort vs heap sort.

Both sort in O(n log n) time. Merge sort needs a temporary array to merge halves, giving O(n) extra space, while heap sort rearranges elements within the original array and needs only O(1) extra space. Two algorithms with equal time can therefore differ in space.

Example 3: Fibonacci numbers.

The naive recursive function fib(n) = fib(n-1) + fib(n-2) recomputes the same values repeatedly and takes exponential time, O(2ⁿ), though only O(n) stack space. Storing computed values in an array (memoisation or dynamic programming) cuts time to O(n) at the cost of O(n) space, and an iterative loop keeping just the last two values achieves O(n) time and O(1) space.

The time-space trade-off.

Programmers often buy speed with memory: a hash table uses extra memory to give average O(1) search, and look-up tables store pre-computed results. On memory-limited devices, the reverse trade is made.

Complexity measures

  • 01Time: number of operations
  • 02Space: memory used
  • 03Linear search O(n) time, O(1) space
  • 04Merge sort O(n) extra space
  • 05Heap sort O(1) extra space
  • 06Fibonacci: trade time for space

Comparison.

- Measures: time complexity counts operations or steps; space complexity counts memory cells.

- Depends on: loops, recursion and comparisons for time; variables, data structures and recursion depth for space.

- Example: bubble sort is O(n²) time but O(1) space.

- Main concern: time for speed and responsiveness; space for memory-limited devices and very large data.

Part (c): Evaluating a postfix expression using a stack

In postfix (Reverse Polish) notation, the operator follows its operands: the infix expression (5 + 3) − 2 is written 5 3 + 2 −. Postfix needs no brackets and no precedence rules, because the order of operations is fixed by position. This makes it ideal for compilers and calculators, which convert infix to postfix and then evaluate it with a stack. (Reference: Seymour Lipschutz, Data Structures, Schaum's Outlines, McGraw-Hill, 1986, Chapter 6.)

Algorithm: EvaluatePostfix(P)

1. Create an empty stack S. 2. Scan the postfix expression P from left to right, one token at a time. 3. If the token is an operand (a number):        PUSH it onto S. 4. If the token is an operator (+, −, ×, /, ^):        a. B = POP(S) (right operand, popped first)        b. A = POP(S) (left operand)        c. R = A operator B        d. PUSH R onto S. 5. Repeat steps 3-4 until the end of P. 6. POP the single value left in S. This is the result. 7. If S does not hold exactly one value at the end, or an operator    finds fewer than two operands, report "invalid expression".

The order of popping matters: the first value popped is the right operand. For "8 2 −" the stack gives B = 2 and A = 8, so the result is 8 − 2 = 6, not 2 − 8.

Worked example.

Evaluate 5 6 2 + × 12 4 / −, which is the postfix form of 5 × (6 + 2) − 12 / 4.

Token Action Stack (bottom → top) 5 push 5 5 6 push 6 5, 6 2 push 2 5, 6, 2 + pop 2, 6; 6 + 2 = 8 5, 8 × pop 8, 5; 5 × 8 = 40 40 12 push 12 40, 12 4 push 4 40, 12, 4 / pop 4, 12; 12 / 4 = 3 40, 3 − pop 3, 40; 40 − 3 = 37 37 end pop result (empty)

Result = 37, which agrees with the infix calculation 5 × 8 − 3 = 37.

Postfix evaluation

  1. 1

    Read token

  2. 2

    Operand: push

  3. 3

    Operator: pop two, compute, push result

  4. 4

    End: single value is the answer

Complexity.

Each token is pushed or popped a constant number of times, so evaluation takes O(n) time for n tokens. The stack holds at most about n/2 + 1 operands, so space is O(n) in the worst case.

Why a stack?

The stack's last-in, first-out order matches postfix exactly: the most recently computed operands are always the ones the next operator needs. The same idea is used by the Java Virtual Machine and many calculators, which evaluate expressions on an operand stack.

Conclusion

Binary search is fast only because sorted order lets it discard half the data at each step; on an unsorted list that guarantee disappears and the search can miss elements that are present, so linear search, sorting first or hashing must be used. Time complexity and space complexity measure two different costs, operations and memory, and good design balances them, as the Fibonacci and sorting examples show. A stack evaluates postfix expressions in linear time with a few simple rules. Together the three parts confirm Wirth's equation: the right algorithm on the right data structure is what makes a program efficient.

Tags

Browse every CSS Computer Science solved past paper or the full library of CSS solved past papers, and compare with the original CSS 2026 Computer Science question paper.

Corrections and discussion

Nothing here yet. Spotted a mistake in this answer, or have a suggestion? Tell us below.

Add your comment, correction or suggestion

Everything is checked by our team before it appears. Please keep it respectful and on topic.

Your rating (optional)

Answered this question differently?

Submit your own answer — approved answers are published with your name.

Submit your answer

Get new solved papers on WhatsApp

Follow the CSS Nexus channel to hear first when new answers are published.

Follow our WhatsApp channel