Suppose that we say:

Cons lst = null;
for (int i = 0; i < n; i++ )
    lst = append( lst, list(i) );

What is the Big O of this code?

  • A: O(1)
  • B: O(n)
  • C: O(n log n)
  • D: O(n2)
  • E: error

    Answer: D

    This code is a trap for the unwary: the Big O of append is the size of its first argument, but the size of the first argument is growing as the loop progresses. This sets up a Bermuda Triangle situation that leaves our ship lost in an O(n2) whirlpool.

    Contents    Page-10    Prev    Next    Page+10    Index