Skip to content

2.6   Exercises

2.6.1   Check your understanding

1.   Time and space costs of iteration and recursion

Both functions below compute \(1 + 2 + \dots + n\) for \(n \ge 1\). Set n to 4, answer the questions by tracing the program in execution order, and then compare the efficiency of the two implementations.

complexity_exercises.py
def sum_iter(n: int) -> int:
    """Iterative sum"""
    res = 0
    for i in range(1, n + 1):
        res += i
    return res

def sum_recur(n: int) -> int:
    """Recursive sum"""
    if n == 1:
        return 1
    return n + sum_recur(n - 1)
complexity_exercises.cpp
/* Iterative sum */
int sumIter(int n) {
    int res = 0;
    for (int i = 1; i <= n; ++i) {
        res += i;
    }
    return res;
}

/* Recursive sum */
int sumRecur(int n) {
    if (n == 1) {
        return 1;
    }
    return n + sumRecur(n - 1);
}
complexity_exercises.java
/* Iterative sum */
int sumIter(int n) {
    int res = 0;
    for (int i = 1; i <= n; i++) {
        res += i;
    }
    return res;
}

/* Recursive sum */
int sumRecur(int n) {
    if (n == 1) {
        return 1;
    }
    return n + sumRecur(n - 1);
}
complexity_exercises.cs
/* Iterative sum */
int SumIter(int n) {
    int res = 0;
    for (int i = 1; i <= n; i++) {
        res += i;
    }
    return res;
}

/* Recursive sum */
int SumRecur(int n) {
    if (n == 1) {
        return 1;
    }
    return n + SumRecur(n - 1);
}
complexity_exercises.go
/* Iterative sum */
func sumIter(n int) int {
    res := 0
    for i := 1; i <= n; i++ {
        res += i
    }
    return res
}

/* Recursive sum */
func sumRecur(n int) int {
    if n == 1 {
        return 1
    }
    return n + sumRecur(n-1)
}
complexity_exercises.swift
/* Iterative sum */
func sumIter(n: Int) -> Int {
    var res = 0
    for i in 1 ... n {
        res += i
    }
    return res
}

/* Recursive sum */
func sumRecur(n: Int) -> Int {
    if n == 1 {
        return 1
    }
    return n + sumRecur(n: n - 1)
}
complexity_exercises.js
/* Iterative sum */
function sumIter(n) {
    let res = 0;
    for (let i = 1; i <= n; i++) {
        res += i;
    }
    return res;
}

/* Recursive sum */
function sumRecur(n) {
    if (n === 1) {
        return 1;
    }
    return n + sumRecur(n - 1);
}
complexity_exercises.ts
/* Iterative sum */
function sumIter(n: number): number {
    let res = 0;
    for (let i = 1; i <= n; i++) {
        res += i;
    }
    return res;
}

/* Recursive sum */
function sumRecur(n: number): number {
    if (n === 1) {
        return 1;
    }
    return n + sumRecur(n - 1);
}
complexity_exercises.dart
/* Iterative sum */
int sumIter(int n) {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res += i;
  }
  return res;
}

/* Recursive sum */
int sumRecur(int n) {
  if (n == 1) {
    return 1;
  }
  return n + sumRecur(n - 1);
}
complexity_exercises.rs
/* Iterative sum */
fn sum_iter(n: i32) -> i32 {
    let mut res = 0;
    for i in 1..=n {
        res += i;
    }
    res
}

/* Recursive sum */
fn sum_recur(n: i32) -> i32 {
    if n == 1 {
        return 1;
    }
    n + sum_recur(n - 1)
}
complexity_exercises.c
/* Iterative sum */
int sumIter(int n) {
    int res = 0;
    for (int i = 1; i <= n; i++) {
        res += i;
    }
    return res;
}

/* Recursive sum */
int sumRecur(int n) {
    if (n == 1) {
        return 1;
    }
    return n + sumRecur(n - 1);
}
complexity_exercises.kt
/* Iterative sum */
fun sumIter(n: Int): Int {
    var res = 0
    for (i in 1..n) {
        res += i
    }
    return res
}

/* Recursive sum */
fun sumRecur(n: Int): Int {
    if (n == 1) {
        return 1
    }
    return n + sumRecur(n - 1)
}
complexity_exercises.rb
### Iterative sum ###
def sum_iter(n)
  res = 0
  for i in 1..n
    res += i
  end
  res
end

### Recursive sum ###
def sum_recur(n)
  return 1 if n == 1

  n + sum_recur(n - 1)
end
  1. When the iterative function runs with n = 4, what value does the accumulator res hold after each iteration?
  2. When the recursive function runs with n = 4, what values does the parameter n take, in order? How is the result computed as the calls return, starting at the deepest level?
  3. What are the time and space complexities of each implementation? Explain using the execution traces from questions 1 and 2.
Sample answer
  1. The loop variable i takes the successive values 1, 2, 3, 4. At the end of each iteration, the running total res is 1, 3, 6, 10, respectively, so the iterative function returns 10.

  2. The parameter n takes the following values in order: 4 → 3 → 2 → 1. The deepest call returns 1. As the remaining calls return, they compute 2 + 1 = 3, 3 + 3 = 6, and 4 + 6 = 10 in order. At the deepest level, all four function calls are still active.

  3. The number of iterations or calls is proportional to \(n\) in both implementations, so both have time complexity \(O(n)\). Their space complexities differ. The iterative version uses a constant number of variables, so its space complexity is \(O(1)\). In the recursive version, earlier calls wait for a result as recursion proceeds toward the base case. The call stack holds up to \(n\) calls at once, so the space complexity is \(O(n)\).

    When analyzing space complexity, count the space used by recursive calls as well as the variables in the code.

2.   Time complexity of three code snippets

Each of the following three snippets takes a positive integer \(n\) as input. Rank them by time complexity, from lowest to highest, and give the complexity of each.

complexity_exercises.py
def linear_loop(n: int) -> int:
    """Linear-time loop"""
    res = 0
    for i in range(n):
        res += i
    return res

def quadratic_loop(n: int) -> int:
    """Quadratic-time loop"""
    res = 0
    for i in range(n):
        for j in range(i, n):
            res += j
    return res

def logarithmic_loop(n: int) -> int:
    """Logarithmic-time loop"""
    while n > 1:
        n //= 2
    return n
complexity_exercises.cpp
/* Linear-time loop */
int linearLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; ++i) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
int quadraticLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
int logarithmicLoop(int n) {
    while (n > 1) {
        n /= 2;
    }
    return n;
}
complexity_exercises.java
/* Linear-time loop */
int linearLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
int quadraticLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
int logarithmicLoop(int n) {
    while (n > 1) {
        n /= 2;
    }
    return n;
}
complexity_exercises.cs
/* Linear-time loop */
int LinearLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
int QuadraticLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
int LogarithmicLoop(int n) {
    while (n > 1) {
        n /= 2;
    }
    return n;
}
complexity_exercises.go
/* Linear-time loop */
func linearLoop(n int) int {
    res := 0
    for i := 0; i < n; i++ {
        res += i
    }
    return res
}

/* Quadratic-time loop */
func quadraticLoop(n int) int {
    res := 0
    for i := 0; i < n; i++ {
        for j := i; j < n; j++ {
            res += j
        }
    }
    return res
}

/* Logarithmic-time loop */
func logarithmicLoop(n int) int {
    for n > 1 {
        n /= 2
    }
    return n
}
complexity_exercises.swift
/* Linear-time loop */
func linearLoop(n: Int) -> Int {
    var res = 0
    for i in 0 ..< n {
        res += i
    }
    return res
}

/* Quadratic-time loop */
func quadraticLoop(n: Int) -> Int {
    var res = 0
    for i in 0 ..< n {
        for j in i ..< n {
            res += j
        }
    }
    return res
}

/* Logarithmic-time loop */
func logarithmicLoop(n: Int) -> Int {
    var n = n
    while n > 1 {
        n /= 2
    }
    return n
}
complexity_exercises.js
/* Linear-time loop */
function linearLoop(n) {
    let res = 0;
    for (let i = 0; i < n; i++) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
function quadraticLoop(n) {
    let res = 0;
    for (let i = 0; i < n; i++) {
        for (let j = i; j < n; j++) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
function logarithmicLoop(n) {
    while (n > 1) {
        n = Math.floor(n / 2);
    }
    return n;
}
complexity_exercises.ts
/* Linear-time loop */
function linearLoop(n: number): number {
    let res = 0;
    for (let i = 0; i < n; i++) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
function quadraticLoop(n: number): number {
    let res = 0;
    for (let i = 0; i < n; i++) {
        for (let j = i; j < n; j++) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
function logarithmicLoop(n: number): number {
    while (n > 1) {
        n = Math.floor(n / 2);
    }
    return n;
}
complexity_exercises.dart
/* Linear-time loop */
int linearLoop(int n) {
  int res = 0;
  for (int i = 0; i < n; i++) {
    res += i;
  }
  return res;
}

/* Quadratic-time loop */
int quadraticLoop(int n) {
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      res += j;
    }
  }
  return res;
}

/* Logarithmic-time loop */
int logarithmicLoop(int n) {
  while (n > 1) {
    n ~/= 2;
  }
  return n;
}
complexity_exercises.rs
/* Linear-time loop */
fn linear_loop(n: i32) -> i32 {
    let mut res = 0;
    for i in 0..n {
        res += i;
    }
    res
}

/* Quadratic-time loop */
fn quadratic_loop(n: i32) -> i32 {
    let mut res = 0;
    for i in 0..n {
        for j in i..n {
            res += j;
        }
    }
    res
}

/* Logarithmic-time loop */
fn logarithmic_loop(mut n: i32) -> i32 {
    while n > 1 {
        n /= 2;
    }
    n
}
complexity_exercises.c
/* Linear-time loop */
int linearLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        res += i;
    }
    return res;
}

/* Quadratic-time loop */
int quadraticLoop(int n) {
    int res = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            res += j;
        }
    }
    return res;
}

/* Logarithmic-time loop */
int logarithmicLoop(int n) {
    while (n > 1) {
        n /= 2;
    }
    return n;
}
complexity_exercises.kt
/* Linear-time loop */
fun linearLoop(n: Int): Int {
    var res = 0
    for (i in 0 until n) {
        res += i
    }
    return res
}

/* Quadratic-time loop */
fun quadraticLoop(n: Int): Int {
    var res = 0
    for (i in 0 until n) {
        for (j in i until n) {
            res += j
        }
    }
    return res
}

/* Logarithmic-time loop */
fun logarithmicLoop(n: Int): Int {
    var value = n
    while (value > 1) {
        value /= 2
    }
    return value
}
complexity_exercises.rb
### Linear-time loop ###
def linear_loop(n)
  res = 0
  for i in 0...n
    res += i
  end
  res
end

### Quadratic-time loop ###
def quadratic_loop(n)
  res = 0
  for i in 0...n
    for j in i...n
      res += j
    end
  end
  res
end

### Logarithmic-time loop ###
def logarithmic_loop(n)
  n /= 2 while n > 1
  n
end
Sample answer

From lowest to highest, they are snippet 3 (\(O(\log n)\)), snippet 1 (\(O(n)\)), and snippet 2 (\(O(n^2)\)). Snippet 3 halves \(n\) on each iteration, so the loop runs about \(\log_2 n\) times. The loop in snippet 1 runs exactly \(n\) times. In snippet 2, the inner loop runs \(n,n-1,\dots,1\) times on successive passes, for a total of \(n(n+1)/2\) iterations. Its time complexity is therefore quadratic.

3.   Which reversal uses less space?

Consider two ways to reverse all the elements of the array nums:

  1. Create an array res of the same length, copy the elements into it in reverse order, and return it.
  2. Move two indices, i and j, inward from the beginning and end, respectively, swapping nums[i] and nums[j] at each step.

    What is the space complexity of each approach? Which one reverses the array in place?

Sample answer
  1. The first approach requires an auxiliary array as long as the input, so its space complexity is \(O(n)\).

  2. The second approach uses only two index variables, so its space complexity is \(O(1)\) and it operates in place.

    Reversing an array in place modifies the input. Prefer this approach only when the input may be modified. If you need to preserve the original array, the cost of copying in the first approach is unavoidable.

2.6.2   Programming exercises

1.   Fibonacci number

The Fibonacci sequence satisfies \(F(0)=0\) and \(F(1)=1\), and for \(n\ge2\), \(F(n)=F(n-1)+F(n-2)\).

Given a nonnegative integer n, use a loop to compute and return \(F(n)\) without recursion.

Hints
  1. Handle n = 0 and n = 1 separately first.
  2. You need only the previous two terms to compute the next one. There is no need to store the whole sequence.
  3. When updating the two variables, keep each old value until you have finished using it.

LeetCode

Share your thoughts, questions, or suggestions in the comments.