CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 3. Variable and Proof
  • 8. Memory Allocation
  • 11. Time Complexity
  • 17. Loop Invariant
Edit on GitHub
  1. Past Year Exam
  2. Final Paper

Final (AY21/22)

PreviousFinal (AY20/21)NextFinal (AY22/23)

Last updated 6 months ago

Problems

Question paper without answers:

Question paper with answers:

3. Variable and Proof

  1. For long variables, it can be negative! Always try to think about negative number when trying to form the counter example.

  2. The keyword "if and only if" needs to prove both forwardly and reversely!

8. Memory Allocation

The code below is a classic memory allocation error:

long *a = malloc(n * sizeof(long *));

Here a should be a pointer pointing to an array of long, but here you assume it should be pointing to to an array of long *. The implementation above is wrong. The correct one should be

long **a = malloc(n * sizeof(long *));

11. Time Complexity

The two recurrence relation below has the same Time Complexity:

T(n)=nT(n/2)+1T(n)=nT(n/2)+n\begin{align*} T(n)&=nT(n/2)+1 \\ T(n)&=nT(n/2)+n \end{align*}T(n)T(n)​=nT(n/2)+1=nT(n/2)+n​

And the time complexity is O(nlogn)O(n^{logn})O(nlogn)

17. Loop Invariant

A good question to try practicing!

To get the strong and correct loop invariant in this question is a bit tricky. In comments, you can clearly see if you use the weak but correct loop invariant a[l] < a[r+1 .. len-1] || a[r] <= a[0 .. l-1]. It will be impossible to do the next question.

So, I would say the tip I get to do this kind of question is that:

  1. Try to find a counter example to prove the given loop invariant is incorrect

  2. To find the correct one, think more about what the code is doing rather than giving out the exact expression directly because sometimes the loop invariant even cannot be expressed in the exact expression. e.g. in this problem, the strong and correct loop invariant is "the minimum element of the array is in a[l .. r]"

182KB
final-2122-s1.pdf
pdf
Question without Answers
196KB
final-2122-s1-with-comments.pdf
pdf
Question with Answers