CSS Prepare

optional · CSS study notes

Pseudo Code (2016)

What Pseudo Code Is

  • Informal, high-level description of an algorithm's logic using structured English + programming constructs (loops, conditionals, assignments) — not tied to any specific language's syntax
  • Bridges the gap between natural-language algorithm description and actual source code

Common Conventions

BEGIN
  READ n
  SET sum = 0
  FOR i = 1 TO n DO
     sum = sum + i
  END FOR
  PRINT sum
END
  • Keywords like BEGIN/END, IF/THEN/ELSE, WHILE, FOR, READ/PRINT used consistently but loosely
  • Indentation shows nesting/control flow, like real code

Why It's Used

  • Language-independent — focuses reviewer's attention on logic, not syntax errors
  • Easier to translate into any programming language (C++, Python, Java) once the logic is verified
  • Standard tool for algorithm design/communication in textbooks, interviews, and CSS papers

Exam Angle

When asked to "write an algorithm" in the CS-optional paper, pseudo code (not full C++ syntax) is usually the expected answer format — focus on clear structured steps over compilable syntax.

📌 Sample & Repeated FPSC Questions (2016–2026)

2026, Paper I, Q3(c) (8 marks) — Write pseudocode to reverse an array using pointers.

Function ReverseArray(arr, n):
    left  <- pointer to arr[0]           // first element
    right <- pointer to arr[n-1]         // last element

    while left < right:                  // pointers haven't crossed
        temp   <- *left
        *left  <- *right
        *right <- temp
        left   <- left + 1
        right  <- right - 1

    return arr

Key reasoning points:

  • Two pointers converge from opposite ends, swapping as they move — runs in O(n) time using only O(1) extra space (in-place, unlike copying into a new reversed array).
  • Loop invariant: at every iteration, everything outside [left, right] is already correctly reversed.
  • Equivalent C++: int* left = arr; int* right = arr + n - 1; inside the same swap-and-converge while loop.

2025, Paper I, Q6(b) (6 marks) — Give pseudocode of implementing Fibonacci sequence recursively. Find the time complexity of this approach and explain how it is calculated.

Function Fibonacci(n):
    if n <= 1:
        return n
    else:
        return Fibonacci(n-1) + Fibonacci(n-2)

Key reasoning points:

  • Each call (for n>1) spawns two further calls, forming a binary recursion tree; the call-count recurrence T(n) = T(n-1) + T(n-2) + O(1) mirrors the Fibonacci recurrence itself, so its solution grows as O(φⁿ) (φ ≈ 1.618) — commonly quoted/bounded as O(2ⁿ).
  • The exponential blow-up happens because the same sub-problems (e.g., Fibonacci(n-3)) get recomputed many times across different branches instead of being cached — memoization or an iterative/DP version brings this down to O(n).

2024, Paper I, Q3(b) (6 marks) — Write pseudocode OR C-language script for a⋅b=∑i=1naibia\cdot b = \sum_{i=1}^{n} a_i b_i (dot product of two n-dimensional vectors).

Function DotProduct(a, b, n):
    dot <- 0
    for i <- 1 to n:
        dot <- dot + a[i] * b[i]
    return dot

Exam angle: FPSC frequently offers "pseudocode OR C/C++" for a math-expression-to-code translation question — pseudocode is faster to write correctly under exam time pressure and scores identically to full syntax, as long as the loop/accumulator logic is unambiguous.